Print contents of an array in C++
This post will discuss how to print the contents of an array in C++.
1. Using Array Indices
A simple solution is to iterate over the elements of an array and print each element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> // Print contents of an array in C++ using array indices int main() { int input[] = { 1, 2, 3, 4, 5 }; size_t n = sizeof(input)/sizeof(input[0]); // loop through the array elements for (size_t i = 0; i < n; i++) { std::cout << input[i] << ' '; } return 0; } |
Output:
1 2 3 4 5
The above code uses the sizeof operator for determining the array size. We can also create a template that deduces the size of the array from its declared type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> template<typename T, size_t n> void print_array(T const(& arr)[n]) { for (size_t i = 0; i < n; i++) { std::cout << arr[i] << ' '; } } // Print contents of an array in C++ using templates int main() { int input[] = { 1, 2, 3, 4, 5 }; print_array(input); return 0; } |
Output:
1 2 3 4 5
2. Using std::copy with std::ostream_iterator function
We can use Ostream iterators to write to an output stream. The idea is to use an output iterator std::ostream_iterator to print array contents to std::cout with the help of std::copy, which takes input iterator to the starting and ending positions of the array and the output iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <algorithm> #include <iterator> // Print contents of an array in C++ using `std::ostream_iterator` int main() { int input[] = { 1, 2, 3, 4, 5 }; int n = sizeof(input)/sizeof(input[0]); std::copy(input, input + n, std::ostream_iterator<int>(std::cout, " ")); // or, use `std::begin` and `std::end` in C++11 // std::copy(std::begin(input), std::end(input), // std::ostream_iterator<int>(std::cout, " ")); return 0; } |
Output:
1 2 3 4 5
With C++17, we can use std::copy with std::experimental::ostream_joiner which is defined in header <experimental/iterator>. It is a single-pass output iterator which can write successive array elements into the std::cout, using the << operator, separated by a delimiter between every two elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <experimental/iterator> int main() { int input[] = { 1, 2, 3, 4, 5 }; std::copy(std::begin(input), std::end(input), std::experimental::make_ostream_joiner(std::cout, " ")); return 0; } |
Output:
1 2 3 4 5
3. Using range-based for-loop
Starting C++11, the recommended approach is to use a range-based for-loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> // Print contents of an array in C++ using range-based for-loop int main() { int input[] = { 1, 2, 3, 4, 5 }; for (const auto &value: input) { std::cout << value << ' '; } return 0; } |
Output:
1 2 3 4 5
4. Using Iterators
We can even use iterators to print array contents even though the array is not an STL container. The idea is to use the std::begin and std::end introduced in C++11. std::start returns an iterator to the beginning and std::end returns an iterator to one past the end of the given array. Since we’re not modifying anything, we should use std::cbegin and std::cend, which returns constant iterators.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> // Print contents of an array in C++ using iterators int main() { int input[] = { 1, 2, 3, 4, 5 }; for (auto it = std::cbegin(input); it != std::cend(input); it++) { std::cout << *it << ' '; } return 0; } |
Output:
1 2 3 4 5
5. Using std::for_each function
Another good alternative is to use the std::for_each algorithm, which applies the specified function on every element within the specified range defined by two iterators. The function can also be an object of a class overloading the ()operator or a lambda expression.
Function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> void print(const int &i) { std::cout << i << ' '; } // Print contents of an array in C++ using `std::for_each` int main() { int input[] = { 1, 2, 3, 4, 5 }; std::for_each(std::begin(input), std::end(input), print); return 0; } |
Output:
1 2 3 4 5
Class
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> #include <algorithm> struct myclass { void operator() (int i) { std::cout << i << ' '; } } ob; // Print contents of an array in C++ using `std::for_each` int main() { int input[] = { 1, 2, 3, 4, 5 }; std::for_each(std::begin(input), std::end(input), ob); return 0; } |
Output:
1 2 3 4 5
Lambda
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> // Print contents of an array in C++ using `std::for_each` int main() { int input[] = { 1, 2, 3, 4, 5 }; std::for_each(std::begin(input), std::end(input), [](const int &e) { std::cout << e << " "; }); return 0; } |
Output:
1 2 3 4 5
That’s all about printing the contents of an array in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)