How to get element in set by index with C++

1 Answer

0 votes
#include <iostream>
#include <set>
   
int main()
{
    std::set<int> st = {12, 100, 89, 55, 45, 99, 19, 61, 70};
   
    std::set<int> :: iterator it = st.begin();
    for (; it != st.end(); it++) {
        std::cout << *it << " ";
    }
   
    it = st.begin();
    int index = 3;
    
    std::advance(it, index);
    
    std::cout << "\n" << *it;
}
   
   
   
   
/*
run:
   
12 19 45 55 61 70 89 99 100 
55
   
*/

 



answered Oct 20, 2022 by avibootz
edited Oct 21, 2022 by avibootz
...