In this C++ tutorial, you will learn how to iterate over a vector using For loop statement, with example program.
C++ Iterate over Elements of Vector using For Loop
To iterate over the elements of a vector using For loop, start at zero index and increment the index by one during each iteration. During the iteration, access the element using index.
Example
In the following C++ program, we define a vector, and iterate over its elements using For loop.
main.cpp
</>
Copy
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> arr{ "apple", "banana", "cherry" };
for ( int index = 0; index < arr.size(); index++ ) {
string element = arr.at(index);
cout << element << endl;
}
}
Output
apple
banana
cherry
Conclusion
In this C++ Tutorial, we learned how to iterate over elements of a vector using For Loop.