盒子
盒子
Posts List
  1. Iterating over a Set using Iterators
  2. Iterating a set in backward direction using reverse_iterator
  3. Iterating over a set using Range base for loop
  4. Iterating over a set using std::for_each and range based for loop

cpp set iter

In this article we will discuss different ways to iterate over a set in C++.

Let’s create a set of strings i.e.

1
2
3
4
5
6
7
// Set of strings
std::set<std::string> setOfStr = {
"jjj",
"khj",
"bca",
"aaa",
"ddd" };

Now let’s iterate over it and print the contents on screen using different methods i.e.

Iterating over a Set using Iterators

set::begin() returns an iterator pointing to the first element in set. Whereas, set::end() returns an iterator past the end of set.

Now to iterate a set in forward direction, we need to create an iterator and initialise it with set::begin(). So that it points to start of set and then we will keep on access and increment the iterator to next till set::end() is reached i.e.

1
2
3
4
5
6
7
8
9
10
11
// Creating a iterator pointing to start of set
std::set<std::string>::iterator it = setOfStr.begin();
// Iterate till the end of set
while (it != setOfStr.end())
{
// Print the element
std::cout << (*it) << ",";
//Increment the iterator
it++;
}

Here we iterated the set in forward direction. Now, let’s see how to iterate in reverse direction.

Iterating a set in backward direction using reverse_iterator

set::rbegin() returns a reverse_iterator pointing to the last element of set. Whereas, set::rend() returns a reverse_iterator pointing to element before the first element.

Now to iterate a set in reverse direction, we need to create an reverse_iterator and initialise it with set::rbegin(). So that it points to the last element of set and then we will keep on access and increment the iterator to next till set::rend() is reached i.e. beginning of set.

1
2
3
4
5
6
7
8
9
10
11
// Creating a reverse iterator pointing to end of set i.e. rbegin
std::set<std::string>::reverse_iterator revIt = setOfStr.rbegin();
// Iterate till the start of set i.e. rend
while (revIt != setOfStr.rend())
{
// Print the element
std::cout << (*revIt) << ",";
//Increment the iterator
revIt++;
}

Iterating over a set using Range base for loop

1
2
3
4
5
6
// Iterate over all elements of set
// using range based for loop
for (auto elem : setOfStr)
{
std::cout << elem << ",";
}

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

1
2
3
4
5
6
// Iterate over all elements using for_each
// and lambda function
std::for_each(setOfStr.begin(), setOfStr.end(), [](const std::string & str)
{
std::cout<<str<<",";
});

Complete example is as follows,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <set>
#include <string>
#include <algorithm>
int main()
{
// Set of strings
std::set<std::string> setOfStr =
{ "jjj", "khj", "bca", "aaa", "ddd" };
std::cout << "*** Iterating Set in Forward Direction using Iterators ***"
<< std::endl;
// Creating a iterator pointing to start of set
std::set<std::string>::iterator it = setOfStr.begin();
// Iterate till the end of set
while (it != setOfStr.end())
{
// Print the element
std::cout << (*it) << ",";
//Increment the iterator
it++;
}
std::cout << std::endl;
std::cout << "*** Iterating Set in Reverse Direction using Iterators ***"
<< std::endl;
// Creating a reverse iterator pointing to end of set i.e. rbegin
std::set<std::string>::reverse_iterator revIt = setOfStr.rbegin();
// Iterate till the start of set i.e. rend
while (revIt != setOfStr.rend())
{
// Print the element
std::cout << (*revIt) << ",";
//Increment the iterator
revIt++;
}
std::cout << std::endl;
std::cout << "*** Iterating Set using range based for loop ***"
<< std::endl;
// Iterate over all elements of set
// using range based for loop
for (auto elem : setOfStr)
{
std::cout << elem << ",";
}
std::cout << std::endl;
std::cout << "*** Iterating Set using for_each algo & Lambda function ***"
<< std::endl;
// Iterate over all elements using for_each
// and lambda function
std::for_each(setOfStr.begin(), setOfStr.end(), [](const std::string & str)
{
std::cout<<str<<",";
});
std::cout << std::endl;
}

Output:

1
2
3
4
5
6
7
8
*** Iterating Set in Forward Direction using Iterators ***
aaa , bca , ddd , jjj , khj ,
*** Iterating Set in Reverse Direction using Iterators ***
khj , jjj , ddd , bca , aaa ,
*** Iterating Set using range based for loop ***
aaa , bca , ddd , jjj , khj ,
*** Iterating Set using for_each algo & Lambda function ***
aaa , bca , ddd , jjj , khj ,