Open In App

Different Methods to Print Elements of Vector in C++

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn different methods to print the elements of vector in C++.

The easiest method to print the elements of vector is by using range based for loop. Let’s take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // Printing elements of vector
    for (auto i : v)
        cout << i << " ";
  
    return 0;
}

Output
1 2 3 4 5 

The range-based loop does not need any iterator or size information. It just needs the name of vector.

There are also some other methods in C++ to print the elements of vector. Some of them are as follows:

Using Traditional Loop

Iterate the vector and print each element of the vector using its index till the index is less than the size. The vector size() method can be used to find the size of the vector.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // Print vector elements 
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
  
    return 0;
}

Output
1 2 3 4 5 

This method provides more control over the printing process.

Using Loop with Iterators

The above method can be modified to work with iterators instead of the index value and size. The vector begin() and vector end() methods to find the iterators to the beginning and the end of the vector. Then a loop can be used to print all elements one by one by incrementing the beginning iterator till it is less than end.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = { 1, 2, 3, 4, 5 };

    // Print vector elements
    for (auto i = v.begin(); i != v.end(); ++i)
        cout << *i << " ";
  
    return 0;
}

Output
1 2 3 4 5 

This method is preferred when you need to work with STL algorithms.

Using for_each()

The for_each() algorithm can be used to apply the given operation to each element in the vector. Apply the print operation to each element of the vector by passing the function to print a single element.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = { 1, 2, 3, 4, 5 };

    // Print vector elements 
    for_each(v.begin(), v.end(), [](int i) {
      	cout << i << " ";
    });
  
    return 0;
}

Output
1 2 3 4 5 

The for_each_n() algorithm is similar to for_each but it allows specifying the number of elements to process. This method is useful for processing a subset of the vector.

Using copy() and ostream_iterator

The ostream_iterator allows the use of output stream as an iterable object and use it in the STL algorithms. Copy the vector elements to ostream_iterator to cout stream using copy() method to print them to the output stream.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = { 1, 2, 3, 4, 5 };

    // Print vector elements 
    copy(v.begin(), v.end(),
         ostream_iterator<int>(cout, " "));
  
    return 0;
}

Output
1 2 3 4 5 

Using accumulate()

The accumulate() function is typically used for summing values, but we can accumulate all the vector elements to a string and print it using a custom operation function.

C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<int> v = { 1, 2, 3, 4, 5 };

    // Acumulate vector to string
    string res = accumulate(v.begin(), v.end(),
                 string(), [](string& s, int n) {
                    return s + (s.empty() ? "" : " ")
                           + to_string(n);
                 });
  
  	// Print elements
    cout << res;
  
    return 0;
}

Output
1 2 3 4 5

This method is more useful for processing the vector elements into a single output, such as concatenating them into a string or other custom operations.

By Overloading << operator

If there is a frequent need to print the vector elements, just overload the << operator as template function at global scope to work directly with the vector object.

C++
#include <bits/stdc++.h>
using namespace std;

// Overload << for vector
template <typename S>
ostream& operator<<(ostream& os,
                    const vector<S>& vector) {
  
    // Printing all the elements using <<
    for (auto i : vector) 
        os << i << " ";
    return os;
}

int main() {
    vector<int> v = {1,2,3,4,5};

  	// Print vector elements
    cout << v;
  
    return 0;
}

Output
1 2 3 4 5 

Next Article
Practice Tags :

Similar Reads