C++ Vector : Print all elements – (6 Ways)

In this article, we will discuss different ways to print all the elements of a vector in C++.


Table of Contents

Suppose we have a vector and we want to print all items of the vector on console. There are different ways to do this, let’s discuss them one by one,

Print all elements of a Vector in C++ by overloading << Operator

We can overload the << operator for a vector as a template function in global scope, to print the contents of the vector of any type.
It will iterate over all items of the vector and print them one by one. Let’s understand by examples,

#include<iostream>
#include<vector>

template <typename T>
std::ostream & operator << (std::ostream & os, const std::vector<T> & vec)
{
    for(auto elem : vec)
    {
        os<<elem<< " ";
    }
    return os;
}

int main()
{
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    std::cout<<vec_of_nums << std::endl;
    return 0;
}

Output:

1 3 4 7 8 9

We overloaded the << operator for vector and then used that to print the contents of vector.

C++: Print vector elements in comma separated manner

If you want to provide a custom separator while printing contents of vector, then you can avoid overloading the << operator. Instead create a separate function to print the contents of a vector with custom separator. We have created a function print_vector(). It accepts two¬† arguments: a vector and a separator string. Inside the print_vector() function, it iterates over all elements of vector and print them one by one separated by provided custom separator string. Let’s understand by example,

#include<iostream>
#include<vector>

/*
Iterate over all elements of vector and print
them one by one, seperated by provided seperated.
*/
template <typename T>
void print_vector(const std::vector<T> & vec, std::string sep=" ")
{
    for(auto elem : vec)
    {
        std::cout<<elem<< sep;
    }
    std::cout<<std::endl;
}


int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector
    print_vector(vec_of_nums, ",");

    // Vector of strings
    std::vector<std::string> vec_of_str{"Hi", "There", "is", "why", "how"};

    // Print all elements in vector
    print_vector(vec_of_str, ",");

    return 0;
}

Output

1,3,4,7,8,9,
Hi,There,is,why,how,

We printed the contents of a vector of integer and then a vector of string using the template function print_vector().

Print all elements of a Vector in C++ using indexing

Unlike previous example, we can iterate over the contents of vector using indexing and print all elements in it one by one. For example,

#include<iostream>
#include<vector>

/*
Iterate over all elements of vector and print
them one by one, seperated by provided seperated.
*/
template <typename T>
void display_vector(const std::vector<T> & vec, std::string sep=" ")
{
    for(int i = 0; i < vec.size() ; i++)
    {
        std::cout<<vec[i]<< sep;
    }
    std::cout<<std::endl;
}

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector
    display_vector(vec_of_nums);

    return 0;
}

Output

1 3 4 7 8 9

Print all elements of a Vector in C++ in one line without for loop

We can print all the items of a vector using a STL algorithm std::copy(). Using this API we can copy all the elements of a vector to the output stream. For example,

#include<iostream>
#include<vector>
#include <iterator>

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector
    std::copy(  vec_of_nums.begin(),
                vec_of_nums.end(),
                std::ostream_iterator<int>(std::cout," "));

    std::cout<<std::endl;

    return 0;
}

Output

1 3 4 7 8 9

Print all elements of a Vector in C++ in one line (Generic Solution)

In previous example, we specifically provided the type of elements in vector while calling the copy() algorithm. But using C++17 experimental::make_ostream_joiner, we can print all elements of a vector without specifying the type of elements in vector. For example,

#include<iostream>
#include<vector>
#include <experimental/iterator>

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector
    std::copy(  vec_of_nums.begin(),
                vec_of_nums.end(),
                std::experimental::make_ostream_joiner(std::cout," ") );

    std::cout<<std::endl;

    return 0;
}

Output

1 3 4 7 8 9

Print all elements of a Vector in C++ in one line using lambda function

Using for_each(), we can apply a lambda function on each element of the vector. Inside the lambda function we can print its value.
Let’s understand with example,

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

int main()
{
    // Vector of integers
    std::vector<int> vec_of_nums{1, 3, 4, 7, 8, 9};
    
    // Print all elements in vector
    std::for_each(  vec_of_nums.begin(),
                    vec_of_nums.end(),
                    [](const auto & elem ) {
                            std::cout<<elem<<" ";
                    });

    std::cout<<std::endl;

    return 0;
}

Output

1 3 4 7 8 9

Here we iterated over all the elements of vector in a single line and printed them one by one.

Advertisements

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top