How to Traverse a Set with const_iterator in C++?
Last Updated :
21 Feb, 2024
In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in C++ STL.
Example
Input:
input_set = {70,10,50,90,60,40,100,30,20,80}
Output:
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Traverse a Set with const_iterator in C++
const_iterator provides a constant reference to the container elements. It means that we can only access the value but cannot modify it. It is useful to prevent unintended changes that may occur while traversing.
The std::set class contains 4 functions that return the constant iterators:
- std::set::cbegin(): Return a const iterator that points to the first element of the set.
- std::set::cend(): Return a const iterator that points just after the last element of the set.
- std::set::crbegin(): Return a const reverse iterator that points to the reverse first element (last element) of the set.
- std::set::crend(): Return a const reverse iterator that points just after the reverse last element(first element) of the set.
We can use the above function to get the constant iterators and traverse the set using loops.
C++ Program to Traverse a Set with const_iterator
C++
// C++ program to traverse the set using the const_iterator.
#include <iostream>
#include <set>
using namespace std;
// Function to display elements of the set
// using rthe const_iterator
void display(set<int> set1)
{
set<int>::const_iterator c_itr;
for (c_itr = set1.cbegin(); c_itr != set1.cend();
c_itr++) {
cout << *c_itr << ", ";
}
}
// Driver Code
int main()
{
// Create an empty set
set<int> set1;
// Insert 10 elements into the set
set1.insert(70);
set1.insert(10);
set1.insert(50);
set1.insert(90);
set1.insert(60);
set1.insert(40);
set1.insert(100);
set1.insert(30);
set1.insert(20);
set1.insert(80);
// Call the display() function
display(set1);
return 0;
}
Output10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Time complexity: O(N), where N is the number of elements in the set.
Auxiliary Space: O(1)
Similar Reads
How to Traverse a List with const_iterator in C++? In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50}
2 min read
How to Traverse a Set with reverse_iterator in C++? In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It provides reverse iterators that make it easier to traverse the set in reverse direction. In this article, we will discuss how to traverse a set with reverse_iter
2 min read
How to Use const_iterator with a Map in C++? In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}
2 min read
How to Traverse a List with reverse_iterator in C++? In C++, lists are containers that allow users to store data in non-contiguous memory locations. List provides reverse iterators that make it easy to traverse the list in reverse order. In this article, we will learn how to traverse a list with reverse_iterator in C++. Example Input: myList = {10,20,
2 min read
How to Traverse Vector using const_iterator in C++? In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out
2 min read