How to Traverse a Vector using for_each Loop in C++? Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 of vector and traversal. myVector ={1,4,9,16,25}Traverse a Vector using for_each Loop in C++The for_each loop allows the users to iterate over the elements of the vector and apply a function to each element of the vector. We can use this loop to traverse the vector by using a function that prints the given value. We can also do any other operation that can be defined inside the Syntaxfor_each(begin, end, func)where, begin: Iterator that denotes the the beginning of vector elements.end: Iterator that denotes the the ending of vector elements.funct: Name of the function to apply on each element of the vector. This function should take a single argument of container element type.C++ Program to Traverse a Vector using for_each LoopIn the following example, we will learn how we can square each element of the vector using the for_each loop in C++. C++ // C++ program to illustrate how we can use a for_each loop // to traverse elements of a vector #include <algorithm> #include <iostream> #include <vector> using namespace std; // declare the function you want to apply on each element of // vector void print(int& num) { cout << num << " "; } void square(int& num) { num *= num; } int main() { // Initialize a vector vector<int> myVector = { 1, 2, 3, 4, 5 }; cout << "Vector Before: "; // Traverse the element using for_each loop for_each(myVector.begin(), myVector.end(), print); cout << endl; // square each element for_each(myVector.begin(), myVector.end(), square); cout << "Vector After: "; // Traverse the element using for_each loop for_each(myVector.begin(), myVector.end(), print); cout << endl; return 0; } OutputVector Before: 1 2 3 4 5 Vector After: 1 4 9 16 25 Time Complexity: O(N) where N is the total number of elements in the vector.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Reverse a Vector using STL in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 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 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 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 How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 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 Like