How to iterate through a Vector without using Iterators in C++
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 Codeint 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 vectorvoid 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 Codeint 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;}Output: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;}Output: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;}Output:-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;}Output:3 2 1 4 5



