Print Array in C++

Print Array in C++

An array is a data structure that stores similar data items at contiguous memory locations under one variable name. The elements in an array can be accessed using indices.

This article discusses the arrays and how to print array in C++.

Introduction of Arrays

For a small number of data items, you can use different variables for each data item, but when the number of instances grows, it becomes difficult to manage them with different variables.

In such conditions, arrays come in handy. The purpose of an array is to store many instances under a single variable name.

You can use arrays to store different data types such as int, float, double, char as well as complex data types such as class and structure objects. The array’s size and memory are both fixed.

Different Methods to Print an Array in C++

To print the arrays in C++, this article discusses the methods given below.

  • By traversing indices
  • Using for_each() function
  • Using range-based for loop
  • Using Iterators
  • Using copy() and ostream_iterator() library functions
  • Print array in C++17 using copy() and make_ostream_joiner() library functions

print array in C++ by Traversing Indices

You can access the array element using indices in the square brackets ([]).

To print the array,

  • Start at index 0 and loop until the index is less than the length of the array.
  • Print the element at the current index.

You can use any of the loops that C++ provides. The example given here uses the for loop.

Syntax of the for loop:

Note that you can skip the first and the third statement if you have initialized the variable and updated it somewhere else. But you can not skip the second statement.

Let’s understand with code.


1 2 3 4 5 6

print array in C++ Using for_each() Function

Apart from the generic looping technique that you have just seen, C++ has the for_each() function that you can use to print the array elements.

The for_each() function lets you define a custom function and applies that function to all the array elements. It is found in the ‘algorithm’ header file.

Let us see the definition of the for_each() function.

The function defines three parameters,

  • first indicates the starting iterator of the array.
  • last indicates the iterator that marks the end of the arrays.
  • fn is the function that the for_each() function applies on each element of the array.

To print the array using the for_each() function,

  • Define a function that takes an integer argument and prints it.
  • Pass the array iterators and the function to the for_each() function.

Let us see the code.


6 5 4 3 2 1

print array in C++ Using Range-Based for Loop

C++11 introduced a new concept of the range-based for loop, that is far superior to the regular ‘for’ loop.

A range-based for loop does not necessitate extensive coding to implement loop iteration. It is a sequential iterator that iterates over each element of the container (from beginning to end).

Let us see the syntax of range-based for loop.

  • Declaration statement declares a variable with the same type as the elements of the array.
  • Range expression defines an expression that represents the array.
  • The loop statement is the body of the range-based for loop that contains statements that will be executed repeatedly until the end of the range expression.

Let’s understand with code.

Note that the code uses the auto keyword to define the type of the variable in the loop. The auto keyword instructs the compiler to automatically detect the data type of the array variables.


2.4 4.5 1.5 3.5 4 6.7

print array in C++ Using Iterators

Iterators are one of the four pillars of the C++ Standard Template Library, also known as the STL. An iterator points to the memory address of the STL container classes. To some extent, you can relate them with a pointer for better understanding.

Iterators serve as a link between algorithms and STL containers, allowing for changes to the data contained within the container. They enable you to iterate over the container, access and assign values and apply various operators to them.

To print the elements of the array, you must understand the following iterators.

  1. begin(): The begin() function returns an iterator to the first element of the array.
  2. end(): The end() function returns an iterator to the element following the container’s last element. Note that this does not point to the actual array element.

To print the array,

  • Make use of any of the loops (including range-based for-loop).
  • Start the loop for begin() iterator and traverse till the end() iterator is reached.
  • Print the element at the current iterator using de-referencing operator (*).

Let’s understand with code.

You can note a new way of declaring and initializing an array in C++. The code actually declares an array using the STL array class.


1 2 3 4 5 6

print array Using ostream_iterator() Function and copy() Function

C++ provides an output iterator ostream_iterator() function that can write to an output stream. Therefore, you can use this function to print the array elements to the console (cout stream).

The definition of the ostream_iterator() function is given below.

You can pass the output stream and separator character to the function to print the array. The function is found in the iterator header file.

However, to be able to print elements, you need another C++ function.

The copy() function lets you copy the elements within a range of iterators to an output iterator. The function is found in the algorithm header file.

The definition of the copy() method is given below.

To print the array, you can call the copy() function with the ostream_iterator() function as the ‘result’ parameter.

Let us see the code.


1 3 5 7 9 11

print array Using C++17 copy() Function and make_ostream_joiner() Function

Similar to the ostream_iterator() function shown in the previous section, make_ostream_joiner() produces a legacy output iterator. The iterator can write the objects to the output stream.

The function was introduced in C++17. It is defined in the experimental/iterator header file and the experimental namespace.

Let us see the code.


1 3 5 7 9 11


The article discussed six different methods to print an array in C++. You can any of the methods according to your convenience.

However, you should note that in several places code uses arr and arr+n for iterators. This is perfectly valid as arr is itself a pointer and holds the address of the first element.

Hope you have enjoyed reading the article. Stay tuned for more such articles.

Happy Learning!



Was this post helpful?

Related Posts

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++ConclusionWas this post helpful? C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferencesWas this post helpful? Maps in C++ store the data in form […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.