![]() |
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:
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_iteratorC++
Output
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, Time complexity: O(N), where N is the number of elements in the set. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |