C++ Program to Iterate Over a Set

Methods to iterate over a set in c++

In this article, we discuss the different methods to iterate over a set in c++. The method that we discuss is given below.

Before going to the methods let’s get a brief understanding of the set in c++. Set in C++ is similar to that of set that we studied in mathematics. Here sets are a type of associative containers in which each element has to be unique. It means suppose if we add 50 two times in a set then it will consider 50 as a single element.

Now let’s understand these methods one by one.

Method 1- Iterating set in forward direction using iterator

In this method, we use two iterators to iterate over a set in c++. Out of these two iterators one iterator pointing to the first element and the other iterator pointing next to the last element. So now we know the position of first and last elements so we now can easily use for or while loop to iterate over the set. Let’s get some insight about the iterators that we are going to use.

  1. set::begin() begin() function is used to return an iterator pointing to the first element of the set container.
  2. set::end() It returns an iterator pointing past the last element of the set container.

Now let’s write the code to iterate over set in the forward direction.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    set<int>::iterator it;
    for(it=st.begin();it!=st.end();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

Output

10 20 30 40 50

Here we also see that we store 50 2 times in a set but while iterating we get 50 1 times so it show that set only store unique elements.

Method 2-Iterating set in reverse direction using reverse iterator

In this method, we use two iterators to iterate over a set in the reverse direction in c++. Out of these two iterators one iterator pointing to the right of the first element and the other iterator pointing to the last element. So now we know the position of first and last elements so we now can easily use for or while loop to iterate over the set. Let’s get some insight about the iterators that we are going to use.

  1. set::rbegin() It returns a reverse iterator pointing to the last element in the container.
  2. set::rend() It returns a reverse iterator pointing to the theoretical element right before the first element in the set container.

Now let’s write the code to iterate over set in the reverse direction.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    set<int>::reverse_iterator it;
    for(it=st.rbegin();it!=st.rend();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

Output

50 40 30 20 10

Method 3:- Iterating over set using for each loop

In this method, we will use for each loop to iterate over a set in c++. Here we use auto keyword. The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. Let’s write the code for it.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    
    for(auto it:st)
    {
        cout<<it<<" ";
    }
    return 0;
}

Output

10 20 30 40 50

So these are the methods to iterate over set in c++.