Skip to content
Related Articles

Related Articles

Improve Article

Different ways to print elements of vector

  • Last Updated : 03 Aug, 2021
Geek Week

Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. The elements of vectors are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.

Different ways to print all elements of a Vector in C++

By using overloading << Operator: By overloading the << operator as template function at global scope, all the elements of the vector can be printed by iterating one by one.

Below is the C++ program to implement the above concept:

C++




// C++ program to print the elements
// of the vector
#include <iostream>
#include <vector>
using namespace std;
 
template <typename S>
ostream& operator<<(ostream& os,
                    const vector<S>& vector)
{
    // Printing all the elements
    // using <<
    for (auto element : vector) {
        os << element << " ";
    }
    return os;
}
 
// Driver Code
int main()
{
    // vector containing integer elements
    vector<int> A = { 10, 20, 30,
                      40, 50, 60 };
 
    cout << A << endl;
 
    return 0;
}
Output
10 20 30 40 50 60 

Printing in a comma-separated manner: By avoiding overloading of the << operator and by creating a separate function, a custom separator can be provided to print the contents of the vector



Below is the C++ program to implement the above approach: 

C++




// C++ program to print the elements
// of the vector using separators
#include <iostream>
#include <vector>
using namespace std;
 
template <typename S>
 
// with_separator() function accepts
// two  arguments i.e., a vector and
// a separator string
void with_separator(const vector<S>& vec,
                    string sep = " ")
{
    // Iterating over all elements of vector
    for (auto elem : vec) {
        cout << elem << sep;
    }
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing integer items
    vector<int> int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in vector
    with_separator(int_vec, ", ");
 
    // Vector containing string
    vector<string> str_vec{ "One", "Two",
                            "Three", "Four",
                            "Five" };
 
    // Printing all elements in vector
    with_separator(str_vec, ", ");
    return 0;
}
Output
10, 20, 30, 40, 50, 60,
One, Two, Three, Four, Five, 

By using indexing: By using the index of the elements in the vector, all the elements can be printed.

Below is the C++ program to implement the above concept:

C++




// C++ program to print the elements
// of the vector by using the index
#include <iostream>
#include <vector>
using namespace std;
 
template <typename S>
void using_index(const vector<S>& vector,
                 string sep = " ")
{
    // Iterating vector by using index
    for (int i = 0; i < vector.size(); i++) {
        // Printing the element at
        // index 'i' of vector
        cout << vector[i] << sep;
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing all integers
    // elements
    vector<int> int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // calling using_index() method
    using_index(int_vec);
 
    return 0;
}
Output
10 20 30 40 50 60 

Printing all elements without for loop by providing element type: All the elements of a vector can be printed using an STL algorithm copy().  All the elements of a vector can be copied to the output stream by providing elements type while calling the copy() algorithm.

Below is the C++ program to implement the above approach:

C++




// C++ program to print the elements
// of the vector by using iterators
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing all integer
    // elements
    vector<int> vec_of_nums{ 10, 20, 30,
                             40, 50, 60 };
 
    // Printing all elements in vector
    // Element type provided int while
    // calling copy()
    copy(vec_of_nums.begin(),
         vec_of_nums.end(),
         ostream_iterator<int>(cout, " "));
 
    return 0;
}
Output
10 20 30 40 50 60 

Using (experimental::make_ostream_joiner) without providing element type: In the Method-1 program, it is observed that while calling the copy() algorithm, the type of elements is specifically provided in the vector. But using C++ 17 experimental::make_ostream_joiner, it is possible to print all elements of a vector without specifying the type of elements in the vector while calling copy().

Below is the C++ program to implement the above approach:

C++




// C++ program to print the elements
// of the vector
#include <experimental/iterator>
#include <iostream>
#include <vector>
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing integers
    vector<int> int_vec = { 10, 20, 30,
                            40, 50, 60 };
 
    // Printing all elements in vector
    // Element type not provided while
    // calling copy()
    copy(int_vec.begin(),
         int_vec.end(),
         experimental::make_ostream_joiner(cout, " "));
 
    cout << endl;
    return 0;
}
Output
10 20 30 40 50 60 

By using the Lambda function: The lambda function can be used on each element of the vector and inside the function, the element can be printed without providing the type of the element.

Below is the C++ program to implement the above approach:

C++




// C++ program to print the elements
// of the vectors
#include <algorithm>
#include <iostream>
#include <vector>
 
// Driver Code
int main()
{
    // Vector containing all
    // integer items
    vector<int> int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in
    // vector
    for_each(int_vec.begin(),
             int_vec.end(),
             [](const auto& elem) {
 
                 // printing one by one element
                 // separated with space
                 cout << elem << " ";
             });
 
    return 0;
}
Output
10 20 30 40 50 60 
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :