How to Iterate Though a Vector in C++

  1. Use for Loop to Iterate Over Vector
  2. Use Range-based Loop to Iterate Over Vector
  3. Use std::for_each Algorithm to Iterate Over Vector

This article will introduce a couple of methods to iterate through the C++ vector using different loops. Note that the sample code uses cout operation to print elements during iteration for better demonstration.

Use for Loop to Iterate Over Vector

The first method is for loop, consisting of a three-part statement each separated with commas. We start by defining and initializing i variable to zero. The next part compares the i variable to the number of elements in the vector, which is retrieved with the size() method. The last part is executed each iteration as a comparison part, and it increments i by one.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};


    for (size_t i = 0; i < str_vec.size(); ++i) {
        cout << str_vec[i] << " - ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

bit - nibble - byte - char - int - long - long long - float - double - long double -

Use Range-based Loop to Iterate Over Vector

for loop can become quite hard to read in some cases, and that’s why there is an alternative structure called a range-based loop. This version is better suited for iterating over-complicated container structures and provides flexible features to access elements. See the following code sample:

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};

    for (auto item : str_vec) {
        cout << item << " - ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Use std::for_each Algorithm to Iterate Over Vector

STL algorithms have extensive features ready for use, and one of those methods is for iteration, which takes as arguments: range and the function to be applied to the range elements. The following example demonstrates how we can declare a function object with the lambda expression and then apply this custom_func to vector elements with one statement.

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::for_each;

int main(){
    vector<int> int_vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    auto custom_func = [](auto &i) { i *= i; };

    for_each(int_vec.begin(), int_vec.end(), custom_func);
    for (auto i : int_vec) cout << i << "; ";
    cout << endl;

    return EXIT_SUCCESS;
}

Related Article - C++ Vector

  • How to Convert Vector to Array in C++
  • comments powered by Disqus