Different ways to print elements of vector in C++

Python

In this article, we are going to learn about Different ways to print elements of vectors in C++.
Vectors are dynamic in nature and the size can grow or shrink at runtime so we need to be careful when trying
to print the elements. We will learn many different ways to print the elements without failing.

1. Using vector.at() inbuilt function


In this example, we are going to use the at() function which is provided by the vector class itself. This function just takes the index location of the index that we want to print. so let us understand this with the below example code.

C++ Program to print vector elements

#include<iostream>
#include<vector>

void printintelements(std::vector <int> const& intvec) {
    
    std::cout << "The elements of Vector are : ";

    for (int i = 0; i < intvec.size(); i++)
        std::cout << intvec.at(i) << ',';
}

int main() {
    
    std::vector<int> intvec = { 1,2,3,4,5 };
    
    printintelements(intvec);
    return 0;
}

Output

The elements of Vector are : 1,2,3,4,5,

2. Print vector element Using the array like index access


The second method is printing the vector element by using the array-like index access. As you might know, we can access the vector elements by index and square brackets. In this example, we are trying to print the vector elements by just iterating over the vector. We are iterating max to the size of the vector so we do not overrun our loop.

C++ Program to print vector elements by using index

// C++ program  2 to print the elements
// of the vector by using the index
#include <iostream>
#include <vector>
using namespace std;

int main()
{

    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

    std::cout << "The elements of Integer Vector are : ";
    for (int i = 0; i < intvec.size(); i++) {
        cout << intvec[i] << ",";
    }
    cout << endl;

    std::cout << "The elements of String Vector are : ";
    for (int i = 0; i < strvec.size(); i++) {
        cout << strvec[i] << ",";
    }

    return 0;
}

Output

The elements of Integer Vector are : 1,2,3,4,5,
The elements of String Vector are : Sachin,Kohli,Dhoni,

3. Print vector Using iterators in vector class


Another method is by using the iterators in vector class. We can iterate over a vector by using the forward iterator
to access each element. When we iterate, we can get the value of each vector element that we can print by using a simple std::cout operator.

C++ Program to print vector

// Program 3 using forward iterator

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

    cout << "The elements of Integer Vector are : ";
    for (auto intit = intvec.cbegin(); intit != intvec.cend(); intit++) {
        std::cout << *intit << ' ';
    }

    cout << endl;

    cout << "The elements of String Vector are : ";
    for (auto strit = strvec.cbegin(); strit != strvec.cend(); strit++) {
        std::cout << *strit << ' ';
    }

    return 0;
}

Output

The elements of Integer Vector are : 1 2 3 4 5
The elements of String Vector are : Sachin Kohli Dhoni

4.Print vector using range based for loop


We can print the elements of a Vector by using a range-based for loop. Here in this example, we are just using a simple
range-based for loop to print each element of an integer and string vector.

C++ Program to print Using ranged based loop

// C++ program 4 to print the elements
// of the vector
#include <iostream>
#include <vector>
using namespace std;


int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

    cout << "The elements of Integer Vector are : ";
    for (auto element : intvec) {
        cout << element << ",";
    }

    cout << endl;

    cout << "The elements of String Vector are : ";
    for (auto strelem : strvec) {
        cout << strelem << ",";
    }

    return 0;
}

Output

The elements of Integer Vector are : 1,2,3,4,5,
The elements of String Vector are : Sachin,Kohli,Dhoni,

5. Print the vector elements with a specific format


If we have a specific need to print the vector elements with a specific format or seperater then we can make use of the
copy() function from the STL library. Using the copy() function, elements can be copied to the output stream and adding a separator of the user’s choice. Let us understand this with the help of the below example.

C++ Program to print vector

// C++ program 5 to print the elements
// of the vector by using iterators
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{

    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

 
    cout << "The elements of Integer Vector are : ";
    copy(intvec.begin(),intvec.end(),ostream_iterator<int>(cout, ","));

    cout << endl;

    cout << "The elements of String Vector are : ";
    copy(strvec.begin(), strvec.end(), ostream_iterator<string>(cout, ","));


    return 0;
}

Output

The elements of Integer Vector are : 1,2,3,4,5,
The elements of String Vector are : Sachin,Kohli,Dhoni,

6. Print vector Using for_each loop lambda


We can make use of the for_each loop and lambda function, in a single line. This will also give us the desired
results and we can print all the elements of the vector.

C++ Program tp print vector

// C++ program 6 to print the elements
// of the vectors
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

    cout << "The elements of Integer Vector are : ";
    for_each(intvec.begin(),
        intvec.end(),
        [](const auto& intelem) {
            cout << intelem << ",";
        });

    cout << endl;

    cout << "The elements of String Vector are : ";
    for_each(strvec.begin(),
        strvec.end(),
        [](const auto& strelem) {
            cout << strelem << ",";
        });

    return 0;
}

Output

The elements of Integer Vector are : 1,2,3,4,5,
The elements of String Vector are : Sachin,Kohli,Dhoni,

If you get an error for auto use in lambda this could be because you are using below C++14 standard.
Then you can change your lambda with exact type like below:

[](const int& intelem) {
cout << intelem << ",";
}

or

[](const string& intelem) {
cout << intelem << ",";
}