Open In App

Different ways to iterate over a set in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Iterate over a set using an iterator.
  2. Iterate over a set in backward direction using reverse_iterator.
  3. Iterate over a set using range-based for loop.
  4. 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:

  1. begin(): Returns an iterator to the first element in the set.
  2. 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++




// C++ program to implement
// the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to display elements
// of a set
void display(set<int> s)
{
  set<int>::iterator itr;
   
  // Displaying set elements
  for (itr = s.begin();
       itr != s.end(); itr++)
  {
    cout << *itr << " ";
  }
 
// Driver code
int main()
{
  // Empty set container
  set<int> s;
 
  // Insert elements in random order
  s.insert(10);
  s.insert(20);
  s.insert(30);
  s.insert(40);
  s.insert(50);
   
  // Invoking function display()
  // to display elements of set
  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:

  1. set::rbegin(): It is a built-in function in C++ STL that returns a reverse iterator pointing to the last element in the container.
  2. 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++




// C++ program to implement
// the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to display elements
// of the set
void display(set<int> s)
{
  set<int>::reverse_iterator itr;
   
  // Displaying elements of the
  // set
  for (itr = s.rbegin();
       itr != s.rend(); itr++)
  {
    cout << *itr << " ";
  }
}
 
// Driver code
int main()
{
  // Empty set container
  set<int> s;
 
  // Insert elements in random order
  s.insert(10);
  s.insert(20);
  s.insert(30);
  s.insert(40);
  s.insert(50);
   
  // Invoking display() function
  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++




// C++ program to implement
// the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to display elements
// of the set
void display(set<int> s)
{
  // Printing the elements of
  // the set
  for (auto itr : s)
  {
    cout << itr << " ";
  
}
// Driver code
int main()
{
  // Empty set container
  set<int> s;
 
  // Insert elements in random order
  s.insert(10);
  s.insert(20);
  s.insert(30);
  s.insert(40);
  s.insert(50);
   
  // Invoking display() function
  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++




// C++ program to implement
// the above approach
#include<bits/stdc++.h>
using namespace std;
 
void print(int x)
{
  cout << x << " ";
}
 
// Function to display the
// elements of set
void display(set<int> s)
{
  for_each(s.begin(), s.end(),
           print);
}
 
// Driver code
  int main()
{
  // Empty set container
  set<int> s;
 
  // Insert elements in random order
  s.insert(10);
  s.insert(20);
  s.insert(30);
  s.insert(40);
  s.insert(50);
   
  // Invoking display() function
  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)



Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads