How to Print an Array in C++

  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

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::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

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 <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

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

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 <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

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

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 <vector>
#include <iterator>

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

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;

Related Article - C++ Array

  • How to Pass 2D Array to a Function in C++
  • How to Return an Array From a Function in C++
  • comments powered by Disqus