1// set::begin/end
2#include <iostream>
3#include <set>
4
5int main ()
6{
7 int myints[] = {75,23,65,42,13};
8 std::set<int> myset (myints,myints+5);
9
10 std::cout << "myset contains:";
11 for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
12 std::cout << ' ' << *it;
13
14 std::cout << '\n';
15
16 return 0;
17}
1//Method 1
2 // Iterate over all elements of set
3 // using range based for loop
4 for (auto& i : mySet)
5 {
6 cout << i << " , ";
7 }
8
9//Method 2
10 // Iterate over all elements using for_each
11 // and lambda function
12 for_each(mySet.begin(), mySet.end(), [](const auto & str)
13 {
14 cout<<str<<", ";
15 });
16
17//Method 3
18 set<string>::iterator it = mySet.begin();
19 // Iterate till the end of set
20 while (it != mySet.end())
21 {
22 // Print the element
23 cout << *it << ", ";
24 //Increment the iterator
25 it++;
26 }
1//Method 1
2 for (auto& i : mySet)
3 {
4 cout << i << " ";
5 }
6
7//Method 2
8 for_each(mySet.begin(), mySet.end(), [](const auto & str)
9 {
10 cout<<str<<" ";
11 });
12
13//Method 3
14 set<string>::iterator it = mySet.begin();
15 while (it != mySet.end()) {
16 cout << *it << " ";
17 it++;
18 }
19//Method 4
20 for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
21 cout <<*it << " ";