How to Use a Range-Based for Loop with a Vector in C++? Last Updated : 05 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 of range-based for loop is: for (const auto &element : container_name) { // Code to be executed for each element}We can use this syntax with our vector to iterator over each element. C++ Program to Print Vector Using Range-Based for Loop C++ // C++ Program to Iterate Over Elements in a Vector Using a // Range-Based For Loop #include <iostream> #include <vector> using namespace std; int main() { // Declare and initialize a vector vector<int> vec = { 10, 20, 30, 40, 50 }; // Use a range-based for loop to iterate over elements // in the vector for (const auto& num : vec) { cout << num << " "; } return 0; } Output10 20 30 40 50 Time Complexity : O(N) where N is the number of elements in vectorAuxilary Space : O(1) Comment More infoAdvertise with us Next Article How to Use a Range-Based for Loop with a Vector in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads Range-Based for Loop with a Set in C++ 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 6Rang 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 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 Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 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 Like