Related Articles
How to iterate through a Vector without using Iterators in C++
• Difficulty Level : Easy
• Last Updated : 01 Jun, 2020

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 ` `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; ` `} `

Output:

```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 ` `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; ` `} `

Output:

```Vector Before Update: 1 2 3 4
Vector After Update: 2 4 6 8
```

• Simple and easy to write code.
• Better and efficient than using iterators method.

• 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:

1. Map:

 `// C++ program to illustrate the iteration ` `// in Map without using iterator ` `#include ` `using` `namespace` `std; ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Declare the map ` `    ``map<``int``, ``int``> Mp; ` ` `  `    ``// Inserting values in Map ` `    ``Mp = 1; ` `    ``Mp = 2; ` `    ``Mp = 3; ` ` `  `    ``// Iterate using value in Map ` `    ``for` `(``auto` `it : Mp) { ` ` `  `        ``// Print the elements ` `        ``cout << it.first << ``' '` `             ``<< it.second << endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```1 1
2 2
3 3
```
2. Map of Vectors:

 `// C++ program to illustrate the iteration ` `// in Map of vectors without using iterator ` `#include ` `using` `namespace` `std; ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Declare the map of vectors ` `    ``map<``int``, vector<``int``> > Mp; ` ` `  `    ``// Temporary vector ` `    ``vector<``int``> temp = { 1, 2, 3 }; ` ` `  `    ``// Inserting values in Map ` `    ``Mp = temp; ` ` `  `    ``temp = { 2, 3, 8, 9 }; ` `    ``Mp = temp; ` ` `  `    ``temp = { 10, -2 }; ` `    ``Mp = temp; ` ` `  `    ``// Iterate using value in Map of vectors ` `    ``for` `(``auto` `it : Mp) { ` ` `  `        ``// Print the elements ` `        ``cout << it.first << ``" -> "``; ` ` `  `        ``// Traverse each vector map ` `        ``// with it.first and print the ` `        ``// elements ` `        ``for` `(``auto` `jt : it.second) { ` `            ``cout << jt << ``' '``; ` `        ``} ` ` `  `        ``cout << endl; ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```1 -> 1 2 3
2 -> 2 3 8 9
3 -> 10 -2
```
3. Set:

 `// C++ program to illustrate the iteration ` `// in set without using iterator ` `#include ` `using` `namespace` `std; ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Declare the set ` `    ``set<``int``> S; ` ` `  `    ``// Inserting values in set ` `    ``S.insert(3); ` `    ``S.insert(-1); ` `    ``S.insert(3); ` `    ``S.insert(4); ` ` `  `    ``// Iterate using value in set ` `    ``for` `(``auto` `it : S) { ` ` `  `        ``// Print the elements ` `        ``cout << it << ``' '``; ` `    ``} ` `    ``return` `0; ` `} `

Output:

```-1 3 4
```
4. Deque:

 `// C++ program to illustrate the iteration ` `// in deque without using iterator ` `#include ` `using` `namespace` `std; ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Declare the deque ` `    ``deque<``int``> dq; ` ` `  `    ``// Inserting values in deque ` `    ``dq.push_front(1); ` `    ``dq.push_front(2); ` `    ``dq.push_front(3); ` ` `  `    ``dq.push_back(4); ` `    ``dq.push_back(5); ` `    ``// Iterate using value in set ` `    ``for` `(``auto` `it : dq) { ` ` `  `        ``// Print the elements ` `        ``cout << it << ``' '``; ` `    ``} ` `    ``return` `0; ` `} `

Output:

```3 2 1 4 5
```

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :