Range-Based for Loop with a Set in C++ Last Updated : 28 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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++. Example:Input:mySet = {1, 2, 3, 4, 6}Output:1 2 3 4 6Range-Based for Loop with a Set in C++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. C++ Program to Traverse a Set using Range-Based for Loop C++ // C++ Program to show how to use a Range-Based for Loop // with a Set #include <iostream> #include <set> using namespace std; int main() { // Duplicate elements will be ignored set<int> mySet = { 3, 1, 4, 1, 5, 9 }; // Iterate over the elements of the set using a // range-based for loop cout << "Elements of set: " << endl; for (const auto& element : mySet) { cout << element << " "; } cout << endl; return 0; } OutputElements after iteration: 1 3 4 5 9 Time complexity: O(N log N)Sapce Complexity: O(1) Comment More infoAdvertise with us Next Article Range-Based for Loop with a Set in C++ anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads Range-Based For Loop with a Map in C++ In C++, a map is a container that stores elements formed by a combination of a key value and a mapped value. 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 map in C++. Example:Input:myMap: { {1, â 2 min read How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o 1 min read Reversed Range-based for loop in C++ with Examples Range-based for loops is an upgraded version of for loops. It is quite similar to for loops which is use in Python. Range-based for loop in C++ is added since C++ 11. We can reverse the process of iterating the loop by using boost::adaptors::reverse() function which is included in boost library Head 2 min read How to Initialize a Vector with Values between a Range in C++? In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read How to Traverse Set using for_each Loop in C++? In C++, STL provides a for_each algorithm which works as a loop for the given range and implements the given function for each element in the range. In this article, we will learn how to use a for_each loop with a set in C++ STL. Traverse Set using for_each Loop in C++To traverse a set using a for_e 2 min read How to Traverse a Set with const_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 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 2 min read How to Initialize a Set with an Array in C++? In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 2 min read How to Create a Set of Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set 2 min read How to Initialize a Map with a Range of Key-Value Pairs in C++? In C++, a map is an associative container that stores elements formed by combining a key and a value. In this article, we will learn how to initialize a map with a range of key-value pairs in C++. Initializing a Map with a Range of Key-Value Pairs in C++To initialize a std::map with a range of key-v 2 min read Like