Iterate over string c++ – C++ iterate over set – Different Ways to Iterate Over a Set in C++

C++ iterate over set: In the previous article, we have discussed about C++ Set example and Tutorial – Part 1. Let us learn how to Iterate Over a Set in C++ Program.

Iterate Over a Set

Print set c++: We are going to see how we can iterate over sets in C++.

Iterating over a Set using Iterators :

Iterate over string c++: We can use the std::set functions to iterate over the set. We will be using the begin( ) to point to the first element and then iterate until we reach end( ) i.e. the end of set.

#include <iostream>
#include <set>
#include <string>
#include <algorithm>

int main()
{
    std::set<std::string> setString = { "abc","fab","top","lol","right"};

    //Iterator pointing to the first element of the set
    std::set<std::string>::iterator iter = setString.begin();
    // Iterate the whole set and print all elements 
    while (iter != setString.end())
    {

        std::cout << (*iter) << " , ";
        //Iterator incrmentation
        iter++;
    }

    return 0;
}
Output :

abc , fab , lol , right , top ,

Iterating a set in backward direction using reverse_iterator :

C++ print set: Just like the previous example, we will be iterating through the begin and end, however here we will be suing the rbegin( ) and rend( ) operator. This rbegin() will point to the last element and the rend( ) will point to the first, returning an iterator pointing to the previous element.

#include <iostream>
#include <set>
#include <string>
#include <algorithm>

int main()
{
    std::set<std::string> setString = {"abc", "fab", "top", "lol", "right"};

    //Iterator that points to the first element of the set
    std::set<std::string>::reverse_iterator revIter = setString.rbegin();
    // Iterate the whole set and print all elements
    while (revIter != setString.rend())
    {

        std::cout << (*revIter) << " , ";
        //Iterator incrmentation
        revIter++;
    }

    return 0;
}
Output :

top , right , lol , fab , abc ,

Iterating over a set using Range based for loop :

#include <iostream>
#include <set>
#include <string>
#include <algorithm>

int main()
{
    std::set<std::string> setString = {"abc", "fab", "top", "lol", "right"};
    //Using range based for loop to iterate the set
    for (auto str : setString)
    {
        std::cout << str << " , ";
    }
    return 0;
}
Output :

abc , fab , lol , right , top ,

Iterating over a set using std::for_each and range based for loop :

#include <iostream>
#include <set>
#include <string>
#include <algorithm>

int main()
{
    std::set<std::string> setString = {"abc", "fab", "top", "lol", "right"};
    //Using range based for loop to iterate the set
    std::for_each(setString.begin(), setString.end(), [](const std::string &elem)
                  { std::cout << elem << " , "; });
    return 0;
}
Output :

abc , fab , lol , right , top ,

Answer these:

  1. Iterate through unordered set c?
  2. Iterate through unordered_set c++?
  3. How to iterate through a pair in c?
  4. How to iterate through a pair in c++?
  5. Iterate through unordered set c
  6. C set iterator to index
  7. Find in set c
  8. Iterate over a set python
  9. Ordered set c++
  10. C++ set iterator
  11. Iterator c++
  12. C++ set iterator to index