In this article, we are going to learn about how to iterate through Vector in C++. We will learn different ways to loop over vector elements. The methods which we are going to learn are:
Ways to iterate through Vector in C++
- By Using for Loop to Iterate Over Vector in C++.
- By using vector iterators.
- By Using a Range-based Loop to Iterate Over Vector.
- By Using the std::for_each Algorithm to Iterate Over Vector.
1. By Using for Loop to Iterate Over Vector in C++.
The first method that we are going to learn is by using for loop to iterate over a vector in C++. We will start iterating from index 0 and continue till we reach the max size of the vector. We can get the max size of the vector by using the size() function.
Let us understand this with the help of the code example below.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intvec{ 1, 2, 3, 4, 5 };
vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };
for (int i = 0; i < intvec.size(); ++i) {
cout << intvec[i] << " - ";
}
cout << endl;
for (int i = 0; i < strvec.size(); ++i) {
cout << strvec[i] << " - ";
}
return 0;
}
Output
1 - 2 - 3 - 4 - 5 -
Sachin - Kohli - Dhoni - Dravid - Sourav -
2. By using iterators to iterate on vectors
Our next example is very similar to the one we just learnt above. In this example also we are making use of for loop.
the difference is that instead of using the index, we are using the vector iterators to iterate over the vector elements.
We are starting our loop by taking the first index that we can get using vector.begin() and go until we reach the index that we get at the end by using vector.end().
Please refer to the code example for this logic.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()c
{
vector<int> intvec{ 1, 2, 3, 4, 5 };
vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };
// For loop using iterators
for (vector<int>::iterator it = intvec.begin(); it != intvec.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
// For loop using iterators
for (vector<string>::iterator it = strvec.begin(); it != strvec.end(); it++)
{
cout << *it << ", ";
}
return 0;
}
Output
1, 2, 3, 4, 5,
Sachin, Kohli, Dhoni, Dravid, Sourav,
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> intvec{ 1, 2, 3, 4, 5 };
vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };
// For loop using auto
for (auto it = intvec.begin(); it != intvec.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
// For loop using auto
for (auto it = strvec.begin(); it != strvec.end(); it++)
{
cout << *it << ", ";
}
return 0;
}
Output
1, 2, 3, 4, 5,
Sachin, Kohli, Dhoni, Dravid, Sourav,
3.Use A Range-based Loop to Iterate Over Vector
In our above two examples, we made use of the for loop to iterate over a vector. With the use of iterators in for loop, the code becomes lengthy. So to shorten our code we are introducing a range-based for loop to iterate over a vector in C++.
By using a range-based for loop, we can iterate over each element in the vector one by one. Let us see the code example to see the compact code.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intvec{ 1, 2, 3, 4, 5 };
vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };
for (auto elem : intvec) {
cout << elem << ", ";
}
cout << endl;
for (auto elem : strvec) {
cout << elem << ", ";
}
return 0;
}
Output
1, 2, 3, 4, 5,
Sachin, Kohli, Dhoni, Dravid, Sourav,
4. By Using the std::for_each Algorithm to Iterate Over Vector
The next method that we are going to use is by using the STL algorithms. In STL we have a std::for_each() algorithm that allows us to iterate over a container and access all its elements.
so by using the for_each() algorithm, we can iterate over the vector very easily.
Let us understand this with the help of the below example.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> intvec{ 1, 2, 3, 4, 5 };
vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };
// Using for_each algorithm with a lambda function
std::for_each(intvec.begin(), intvec.end(), [](int const& value) {
std::cout << value << "\n";
});
cout << endl;
// Using for_each algorithm with a lambda function
std::for_each(strvec.begin(), strvec.end(), [](string const& value) {
std::cout << value << "\n";
});
return 0;
}
Output
1
2
3
4
5
Sachin
Kohli
Dhoni
Dravid
Sourav
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> intvec{ 1, 2, 3, 4, 5 };
auto lambda_func = [](int& i) { i *= i; };
for_each(intvec.begin(), intvec.end(), lambda_func);
for (auto i : intvec)
cout << i << "; ";
cout << endl;
return 0;
}
Output
1; 4; 9; 16; 25;