C++ Iterate Over Vector – 4 Ways

Vector comes under STL. It can be termed as a dynamic array i.e, we can add or remove elements in vectors even in runtime. This flexibility with values is not possible in the arrays.

Vectors aren’t ordered in increasing or decreasing order, although they can be easily accessed by iterators.

There are different ways through which we can traverse through a vector. In this blog, we will be exploring all those ways.

1. Using for Loop

We will iterate through the vector by accessing all the indexes one after another inside the for loop.

Let us see the code for a better overview.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

   //Taking the below vector as an example.

   vector<int> myvector = {10, 50, 20, 60, 43, 32};

   cout << "The elements present in the above vector are: " << "\n";

   for(int i=0;i<myvector.size();i++) {

      cout << myvector[i] << " ";

      // i is represented as an index.

   }

   cout << "\n";

   return 0;

}

Output:

The elements present in the above vector are:

10 50 20 60 43 32

2. Using while Loop with at() Method

We will iterate through the vector by accessing all the indexes one after another using the at method inside while loop. Let us see the code for a better overview.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

   //Taking the below vector as an example.

   vector<int> myvector = {10, 50, 20, 60, 43, 32};

   cout << "The elements present in the above vector are: " << "\n";

   int lastindex=0;

   while ( lastindex < myvector.size()) {

      cout << myvector.at(lastindex) << " ";

      lastindex++;

   }

   cout << "\n";

   return 0;

}

Output:

The elements present in the above vector are:

10 50 20 60 43 32

3. Using an Iterator

An iterator is declared which points to the beginning of the vector, and we increase the iterator by 1 till it doesn’t reach the end of the vector. Let us see the code for a better overview.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

   //Taking the below vector as an example.

   vector<int> myvector = {10, 50, 20, 60, 43, 32};

   cout << "The elements present in the above vector are: " << "\n";

   vector<int>::iterator my_iterator = myvector.begin();

   for( my_iterator; my_iterator != myvector.end(); my_iterator++) {

      cout << *my_iterator << " ";

   }

   cout << "\n";

   return 0;

}

Output:

The elements present in the above vector are:

10 50 20 60 43 32

4. Using auto Keyword with for Loop

The auto keyword adjusts the datatype according to the needs. It can only be used with the for loop. Let us see the code for a better overview.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

   //Taking the below vector as an example.

   vector<int> myvector = {10, 50, 20, 60, 43, 32};

   cout << "The elements present in the above vector are: " << "\n";

   for (auto & value : myvector)

   {

      cout << value << " ";

   }

   cout << "\n";

   return 0;

}

Output:

The elements present in the above vector are:

10 50 20 60 43 32

There are some similar ways in which we can use while, do-while loop but the main logic is the same. So I hope you have understood this blog clearly, feel free to comment below if you still have any doubts.

1 thought on “C++ Iterate Over Vector – 4 Ways”

Leave a Comment

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