Prerequisite: C++ STL, Iterators in C++ STL
The iterator is not the only way to iterate through any STL container. There exists a better and efficient way to iterate through vector without using iterators. It can be iterated using the values stored in any container. Below is the syntax for the same for vectors:
Syntax:
for(auto itr : vector_name)
Explanation: Here itr is the value stored in vector which is used to traverse vectors. Below is the program to illustrate the same:
// C++ program to illustrate the above // topic #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Declare the vector vector<int> arr = { 1, 2, 3, 4 }; // Traversing the vector using // values directly for (auto& it : arr) { // Print the values cout << it << ' '; } return 0; } |
1 2 3 4
Updating values in vector: For updating values in a vector without using iterators traverse the values stored in vector using reference and updated the value. Below is the syntax for the same:
Syntax:
for(auto &itr : vector_name)
Explanation: Here itr is an address to the value stored in vector which is used to traverse vectors. Below is the program to illustrate the same:
// C++ program to illustrate the updation // in vector without using iterator #include <bits/stdc++.h> using namespace std; // Function to update the value in vector void updateVector(vector<int> arr) { cout << "Vector Before Update: "; for (auto& it : arr) { cout << it << ' '; } // Traverse using the reference to value // and multiply each value by 2 for (auto& it : arr) { it *= 2; } cout << "\nVector After Update: "; // Print vector elements for (auto& it : arr) { cout << it << ' '; } } // Driver Code int main() { // Declare the vector vector<int> arr = { 1, 2, 3, 4 }; // Function Call updateVector(arr); return 0; } |
Vector Before Update: 1 2 3 4 Vector After Update: 2 4 6 8
Advantages:
- Simple and easy to write code.
- Better and efficient than using iterators method.
Disadvantages:
- It iterates only in forward direction.
- Keeps no counter i.e., We cannot find the index of any element with this traversal. For counting the element, the counter have to taken explicitly.
We can also iterate using the same traversal in many different Containers in C++. Below are the illustration for the same:
- Map:
// C++ program to illustrate the iteration// in Map without using iterator#include <bits/stdc++.h>usingnamespacestd;// Driver Codeintmain(){// Declare the mapmap<int,int> Mp;// Inserting values in MapMp[1] = 1;Mp[2] = 2;Mp[3] = 3;// Iterate using value in Mapfor(autoit : Mp) {// Print the elementscout << it.first <<' '<< it.second << endl;}return0;}chevron_rightfilter_noneOutput:1 1 2 2 3 3
- Map of Vectors:
// C++ program to illustrate the iteration// in Map of vectors without using iterator#include <bits/stdc++.h>usingnamespacestd;// Driver Codeintmain(){// Declare the map of vectorsmap<int, vector<int> > Mp;// Temporary vectorvector<int> temp = { 1, 2, 3 };// Inserting values in MapMp[1] = temp;temp = { 2, 3, 8, 9 };Mp[2] = temp;temp = { 10, -2 };Mp[3] = temp;// Iterate using value in Map of vectorsfor(autoit : Mp) {// Print the elementscout << it.first <<" -> ";// Traverse each vector map// with it.first and print the// elementsfor(autojt : it.second) {cout << jt <<' ';}cout << endl;}return0;}chevron_rightfilter_noneOutput:1 -> 1 2 3 2 -> 2 3 8 9 3 -> 10 -2
- Set:
// C++ program to illustrate the iteration// in set without using iterator#include <bits/stdc++.h>usingnamespacestd;// Driver Codeintmain(){// Declare the setset<int> S;// Inserting values in setS.insert(3);S.insert(-1);S.insert(3);S.insert(4);// Iterate using value in setfor(autoit : S) {// Print the elementscout << it <<' ';}return0;}chevron_rightfilter_noneOutput:-1 3 4
- Deque:
// C++ program to illustrate the iteration// in deque without using iterator#include <bits/stdc++.h>usingnamespacestd;// Driver Codeintmain(){// Declare the dequedeque<int> dq;// Inserting values in dequedq.push_front(1);dq.push_front(2);dq.push_front(3);dq.push_back(4);dq.push_back(5);// Iterate using value in setfor(autoit : dq) {// Print the elementscout << it <<' ';}return0;}chevron_rightfilter_noneOutput:3 2 1 4 5
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- How to Iterate through a String word by word in C++
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::empty() and vector::size() in C++ STL
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- How to iterate over a 2D list (list of lists) in Java
- How to iterate over the elements of an std::tuple in C++
- Iterate over characters of a string in C++
- Iterators in C++ STL
- Random-access Iterators in C++
- Forward Iterators in C++
- Output Iterators in C++
- Bidirectional Iterators in C++
- Introduction to Iterators in C++
- Difference between Iterators and Pointers in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

