![]() |
VOOZH | about |
In C++, a set is a container that stores unique elements in a particular order. A range-based for loop is a feature added in C++11 to iterate over containers. In this article, we will learn how to use a range-based for loop with a set in C++.
Input:
mySet = {1, 2, 3, 4, 6}
Output:
1 2 3 4 6
We can use a range-based for loop with a set in C++ for any operation that can be done using loops. It just requires the name of the set container and iterates each element in the set. In the below program, we have used the range-based for loop to traverse and print the set container.
Elements after iteration: 1 3 4 5 9
Time complexity: O(N log N)
Sapce Complexity: O(1)