How to Print an Array in C++

Jinku Hu Feb 02, 2024
  1. Use a Range-Based Loop to Print Out an Array
  2. Use the copy Function to Print Out an Array
  3. Use for_each Algorithm to Print Out an Array
How to Print an Array in C++

This article will introduce C++ methods to print out an array’s elements to console.

Use a Range-Based Loop to Print Out an Array

This method is a typical for loop only with a modern C++11 range-based style. Range-based iteration provides an option to access elements with custom specifiers like: by const reference (auto const& i), by value (auto i), or by forwarding reference (auto&& i). The benefits of this method over the traditional for loop are ease of use and readability.

#include <iostream>
#include <vector>

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

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

  for (auto const& value : arr) cout << value << "; ";
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

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

Use the copy Function to Print Out an Array

copy() function is implemented in the STL <algorithm> library and offers a powerful tool for range-based operations. copy takes start and endpoint iterators of the range as the first two parameters. In this case, we pass an output stream iterator as the third argument to output array elements to the console.

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

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

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

  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

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

As an alternative, we can easily reimplement the above example to output the array elements in reverse order. We modify the first two arguments of the copy() function and substitute them with rbegin/rend function calls to achieve this.

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

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

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

  copy(arr.rbegin(), arr.rend(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

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

Use for_each Algorithm to Print Out an Array

for_each is another powerful algorithm from the STL library. It can apply the given function object to every element of the range. In this case, we define a lambda expression as a custom_func variable and pass it to the for_each method to operate on the given array elements.

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

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

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

  auto custom_func = [](auto &i) { cout << i << "; "; };
  for_each(arr.begin(), arr.end(), custom_func);
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

1; 2; 3; 4; 5; 6; 7; 8; 9; 10;
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++ Array