How to Print Out the Contents of a Vector in C++

Jinku Hu Feb 02, 2024
  1. Use the for Loop With Element Access Notation to Print Out Vector Contents
  2. Use Range-Based Loop With Element Access Notation to Print Out Vector Contents
  3. Use std::copy to Print Out Vector Contents
  4. Use std::for_each to Print Out Vector Contents
How to Print Out the Contents of a Vector in C++

This article will introduce several methods of how to print out vector contents in C++.

Use the for Loop With Element Access Notation to Print Out Vector Contents

vector elements can be accessed with the at() method or the [] operator. In this solution, we demonstrate both of them and print the contents with the cout stream. At first, we initialize the vector variable with arbitrary integers and then loop through its elements one by one to print out stream.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
  vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  for (size_t i = 0; i < int_vec.size(); ++i) {
    cout << int_vec.at(i) << "; ";
  }
  cout << endl;

  for (size_t i = 0; i < int_vec.size(); ++i) {
    cout << int_vec[i] << "; ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

1; 2; 3; 4; 5; 6; 7; 8; 9; 10;
1; 2; 3; 4; 5; 6; 7; 8; 9; 10;

Use Range-Based Loop With Element Access Notation to Print Out Vector Contents

The previous solution is relatively straightforward but looks quite verbose. Contemporary C++ provides more flexible ways to express iterations, one of them being the range-based loop. It is considered more readable when loops do relatively effortless operations and don’t need to be parallelized.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
  vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  for (const auto &item : int_vec) {
    cout << item << "; ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Use std::copy to Print Out Vector Contents

A rather advanced method of doing the vector iteration and output operations in one statement is calling the copy function, defined in the <algorithm> library. This method takes a vector range specified with iterators, and as a third argument, we pass ostream_iterator to redirect contents of the range to the cout stream.

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

using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::ostream_iterator;
using std::vector;

int main() {
  vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  copy(int_vec.begin(), int_vec.end(), ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Use std::for_each to Print Out Vector Contents

Another STL algorithm for doing the iteration and output operations in a single statement is for_each. This method can apply a certain function object to each element of the specified range, which is a powerful tool to have. In this case, we pass the lambda expression, which prints the elements to the output stream.

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

using std::cin;
using std::cout;
using std::endl;
using std::for_each;
using std::vector;

int main() {
  vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  for_each(int_vec.begin(), int_vec.end(),
           [](const int& n) { cout << n << "; "; });
  cout << endl;

  return EXIT_SUCCESS;
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Vector