How to Traverse Set using for_each Loop in C++? Last Updated : 27 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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_each loop in C++ STL, we will need a callable function that prints the elements of the set. We can define this callable as a functor, lambda expression, and also a normal function. This function will be applied to all the elements of the set. Syntax of for_each Loopfor_each (InputIterator start_iter, InputIterator last_iter, Function fnc)here, start_iter: The beginning position from where function operations have 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 whose operation would be applied to each element. C++ Program to Use a for_each Loop with a Set C++ // C++ Program to use a for_each Loop to traverse a Set #include <algorithm> #include <iostream> #include <set> using namespace std; // Driver Code int main() { set<int> mySet = { 1, 2, 3, 4, 5 }; // Define a lambda function to print each element auto printElement = [](int element) { cout << element << " "; }; // Use std::for_each to iterate over the set and apply // the lambda function to each element for_each(mySet.begin(), mySet.end(), printElement); cout << endl; return 0; } Output1 2 3 4 5 Time Complexity: O(N)Space Complexity: O(1) We can use the for_each loop for any task that we can do with other types of loops. Comment More infoAdvertise with us Next Article How to Traverse Vector Using const_reverse_iterator in C++? 21mcsrltd Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Traverse a Vector using for_each Loop in C++? In C++, vectors are dynamic containers that can change their size automatically during the insertion and deletion of elements. In this article, we will learn how we can use a for_each loop to traverse elements of a vector in C++. Example Input: myVector = {1,2,3,4,5} Output: // Squared each element 2 min read How to Traverse a Map Using Iterator in C++? In C++, maps are associative containers that store elements where each element has a key value and a mapped value. An iterator is a pointer-like object that allows traversing through the elements of a container like a map. In this article, we will learn how to traverse a map using an iterator in C++ 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 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 Traverse Vector Using const_reverse_iterator in C++? In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato 2 min read 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 Like