How to iterate through a Vector without using Iterators in C++

The iterator isn’t the best way to repeat through any STL compartment. There exists a superior and proficient method for emphasizing through vector without utilizing iterators. It tends to be iterated utilizing the qualities put away in any holder. The following is the sentence structure for something very similar for vectors:

Syntax:

for(auto itr : vector_name)

Clarification: Here itr is the worth put away in vector which is utilized to cross vectors. The following is the program to delineate something similar:

// 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;
}
Output:

1 2 3 4

Refreshing qualities in vector: For refreshing qualities in a vector without utilizing iterators cross the qualities put away in vector utilizing reference and refreshed the worth. The following is the sentence structure for something similar:

Syntax:

for(auto &itr : vector_name)

Clarification: Here itr is a location to the worth put away in vector which is utilized to cross vectors. The following is the program to outline something very similar:

// 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;
}
Output:

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

Table of Contents

Benefits:

  • Basic and simple to compose code.
  • Preferred and productive over utilizing iterators strategy.

Hindrances:

  • It repeats just in forward course.
  • Keeps no counter i.e., We can’t track down the file of any component with this crossing. For counting the component, the counter need to taken expressly.
  • We can likewise repeat involving similar crossing in various Containers in C++. The following are the representation for the equivalent:

Map:

// C++ program to illustrate the iteration
// in Map without using iterator
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
 
    // Declare the map
    map<int, int> Mp;
 
    // Inserting values in Map
    Mp[1] = 1;
    Mp[2] = 2;
    Mp[3] = 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

Map of Vectors:

// C++ program to illustrate the iteration
// in Map of vectors without using iterator
#include <bits/stdc++.h>
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[1] = temp;
 
    temp = { 2, 3, 8, 9 };
    Mp[2] = temp;
 
    temp = { 10, -2 };
    Mp[3] = 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

Set:

// C++ program to illustrate the iteration
// in set without using iterator
#include <bits/stdc++.h>
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

Deque:

// C++ program to illustrate the iteration
// in deque without using iterator
#include <bits/stdc++.h>
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

Also Read: How to Learn Programming?

Leave a Reply

Your email address will not be published. Required fields are marked *