Print all elements in vector c++ – C++ Vector : Print All Elements – (6 Ways)

Print all elements in vector c++: In the previous article, we have discussed about 5 Different ways to Initialize a vector in C++. Let us learn Vector : Print All Elements – (6 Ways) in C++ Program.

Printing Elements of Vectors in 6 Ways

Print contents of vector c++: This article is all about various ways to print all the elements of a vector in C++.

Let’s see one by one.

Method -1 : Printing  elements of a Vector overloading << Operator :

C++ vector print: By overloading  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 .

#include<iostream>
#include<vector>
template <typename temp>
std::ostream & operator << (std::ostream & os, const std::vector<temp> & x)
{
    for(auto elem : x)
    {
        os<<elem<< " ";
    }
    return os;
}
int main()
{
    std::vector<int> vec{11, 23 , 87 , 95, 44};
    std :: cout << "vectors : "  ;
    std::cout<<vec << std::endl;
    return 0;
}
Output :

vectors : 11 23 87 95 44

Method-2 : Printing  vector element by  custom separated manner :

Print elements of vector c++: If we want to provide a custom separator while printing elements  of vector, then we can create a function which will  accept  two  arguments i.e.  vector & separator string. Inside this,  it iterates over all elements of vector and print them one by one separated by provided custom separator .

#include<iostream>
#include<vector>
//Iterating  over all elements of vector and printing them one by one, separated by provided separated.
template <typename temp>
void printvec(const std::vector<temp> & vec, std::string sep=" ")
{
 for(auto ele : vec)
   	 {  std::cout<<ele<< sep;  }
    std::cout<<std::endl;
}
int main()
{
    // declaration of  Vector of integers
    std::vector<int> vec1{12, 33, 44, 76, 87, 97};
    // printing all elements
    std :: cout << "Vector with numbers : " ;
    printvec(vec1, " , ");
    // declaration of  Vector of strings
    std::vector<std::string> vec2{ "what ",  "a", "beautiful", " day!!! "};
    // printing all elements
    std :: cout << "Vector with strings : ";
    printvec(vec2, " , ");
    return 0;
}
Output :

Vector with numbers : 12 , 33 , 44 , 76 , 87 , 97 , 
Vector with strings : what , a , beautiful , day!!! ,

Method-3 : Printing of  all elements using indexing :

Print a vector c++: We can  also iterate over the contents of vector using indexing and can  print all elements in it one by one.

#include<iostream>
#include<vector>
//Iterating  all elements of vector and printing them one by one, separated by provided separated.
template <typename temp>
void dispvec(const std::vector<temp> & v, std::string sep=" ")
{
    for(int x = 0; x < v.size() ; x++)
    {
        std::cout<<v[x]<< sep;
    }
    std::cout<<std::endl;
}
int main()
{
    //  declaration of Vector of integers
    std::vector<int> vec1{12, 34, 47, 72, 85, 96};
    // Printing of  all elements in vector
    std :: cout << "vectors : " ;
    dispvec(vec1);
    return 0;
}
Output :

vectors : 12 34 47 72 85 96

Example-4 : Printing  elements of a Vector without for loop :

How to print a vector c++: We can print  a vector using a STL algorithm i.e. std::copy(). Using this we can copy all the elements of a vector to the o/p stream.

#include<iostream>
#include<vector>
#include <iterator>
int main()
{
    //  declaration of Vector of integers
    std :: vector<int> vec1{15, 63, 47, 77, 83, 39};
    // Printing  all elements in vector
    std :: cout << "vectors : "  ;
    std ::copy( vec1.begin() , vec1.end(), std :: ostream_iterator<int>(std::cout," "));
    std ::cout<<std :: endl;
    return 0;
}
Output :

vectors : 15 63 47 77 83 39

Example-5 : Generic Solution of Print all elements of a Vector in one line :

Vector print c++: By  using C++17 experimental :: make_ostream_joiner , we can also  print elements of a vector without specifying the type of elements .

#include<iostream>
#include<vector>
#include <experimental/iterator>
int main()
{
    // declaration of  Vector of integers
    std::vector<int> vec{12, 33, 46, 27, 84, 95};
    // Printing of  all elements in vector
    std:: cout << "vectors : " ;
    std::copy(vec.begin(),vec.end(),std::experimental::make_ostream_joiner(std::cout,"  ") );
    std::cout<<std::endl;
    return 0;
}
Output :

vectors : 12 33 46 27 84 95

Example-6 : Printing  elements of a Vector using lambda function :

Output vector c++: We can also apply a lambda function on each element by Using for_each () , this  we can print its value.

#include<iostream>
#include<vector>
#include <algorithm>
int main()
{
    //  declaration of Vector of integers
    std::vector<int> vec{1, 3, 4, 7, 8, 9};
    // Printing of  all elements in vector
    std :: cout << "vectors : " ;
    std::for_each(  vec.begin(),
                    vec.end(),
                    [](const auto & x ) {
                            std::cout<< x <<" ";
                    });
    std::cout<<std::endl;
    return 0;
}
Output :

vectors : 1 3 4 7 8 9