Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order.
Syntax:
set<datatype> setname;
Here,
Datatype: Set can take any data type depending on the values, e.g. int, char, float, etc.
This article focuses on discussing all the methods that can be used to iterate over a set in C++. The following methods will be discussed in this article:
- Iterate over a set using an iterator.
- Iterate over a set in backward direction using reverse_iterator.
- Iterate over a set using range-based for loop.
- Iterate over a set using for_each loop.
Let’s start discussing each of these methods in detail.
Iterating over a set using iterator.
In this method, an iterator itr is created and initialized using begin() function which will point to the first element, and after every iteration, itr points to the next element in a set and it will continue to iterate until it reaches the end of the set.
The following methods will be used in this approach:
- begin(): Returns an iterator to the first element in the set.
- end(): Returns an iterator to the theoretical element that follows the last element in the set.
Below is the C++ program to implement the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void display(set< int > s)
{
set< int >::iterator itr;
for (itr = s.begin();
itr != s.end(); itr++)
{
cout << *itr << " " ;
}
}
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
display(s);
return 0;
}
|
Output:
10 20 30 40 50
Iterate over a set in backward direction using reverse_iterator
In this approach, a reverse_iterator itr is created and initialized using rbegin() function which will point to the last element in a set, and after every iteration, itr points to the next element in a backward direction in a set and it will continue to iterate until it reaches the beginning of the set.
The following functions are used in this approach:
- set::rbegin(): It is a built-in function in C++ STL that returns a reverse iterator pointing to the last element in the container.
- set::rend(): It is an inbuilt function in C++ STL that returns a reverse iterator pointing to the theoretical element right before the first element in the set container.
Below is the C++ program to implement the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void display(set< int > s)
{
set< int >::reverse_iterator itr;
for (itr = s.rbegin();
itr != s.rend(); itr++)
{
cout << *itr << " " ;
}
}
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
display(s);
return 0;
}
|
Output:
50 40 30 20 10
Iterate over a set using range-based for loop
In this method, a range-based for loop will be used to iterate over all the elements in a set in a forward direction.
Syntax:
for ( range_declaration : range_expression )
loop_statement
Parameters :
range_declaration :
A declaration of a named variable, whose type is the type of the element of the sequence represented by range_expression, or a reference to that type. Often uses the auto specifier for automatic type deduction.
range_expression:
Any expression that represents a suitable sequence or a braced-init-list.
loop_statement:
Any statement, typically a compound statement, which is the body of the loop.
Below is the C++ program to implement the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void display(set< int > s)
{
for ( auto itr : s)
{
cout << itr << " " ;
}
}
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
display(s);
return 0;
}
|
Output:
10 20 30 40 50
Iterate over a set using for_each loop
In this approach, a for_each loop accepts a function that executes over each of the container elements.
Syntax:
for_each (InputIterator start_iter, InputIterator last_iter, Function fnc)
start_iter: The beginning position from where function operations has to be executed.
last_iter: The ending position till where function has to be executed.
fnc/obj_fnc: The 3rd argument is a function or an object function which operation would be applied to each element.
Below is the C++ program to implement the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void print( int x)
{
cout << x << " " ;
}
void display(set< int > s)
{
for_each(s.begin(), s.end(),
print);
}
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
display(s);
return 0;
}
|
Output:
10 20 30 40 50
Time complexity: O(N) // N is the size of the set.
Auxiliary Space: O(N)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Feb, 2023
Like Article
Save Article