In this tutorial, you will learn about the various new features that the latest version of C++ provides programmers. These features are as a whole comes under the advanced mechanisms and concepts that C++ provides in its last developing phase.

What are arrays in C++11?

The arrays in C++11 are the instance or examples of the container class array<> — that models a static array. This encloses an ordinary static C style array which provides the interface and structure of an STL container. Conceptually, an array can be defined a series of consecutive elements having constant size. So, you can neither add elements nor remove elements for changing the size. Only a substitute of any element values can be achievable.

The class array<>, launched to this new C++ standard library is an outcome of a useful wrapper class for ordinary C style arrays which Bjarne Stroustrup introduced in older C++ standards. It is safer and includes no worse performance than that of a normal array. For using an array, you must include the header file <array> --

#include <array>

Initialization of C++11 Arrays:

In case of initialization of arrays in C++11, class array<> got some unique semantics. These are listed in the form of examples:

  • std::array<int, 4> g; // Oopz!: the elements of g got undefined value
  • std::array<int, 4> g = {}; // OK: all elements of g have value 0 (int())
  • std::array<int, 6> colln = { 62, 342, 78, 212, 94, 8 };
  • std::array<int, 10> k2 = { 56 }; // one element with value 56 …. followed by rest 9 elements as value 0
  • std::array<int, 4> g6 = { 1, 2, 3, 4, 5, 6 }; // ERROR: too many values

Here is a simple example, showing the implementation of new C++ array taking three values in an array and printing its value:

#include <array>
#include <iostream>

int main()
    std::array<int, 3> arr = {2, 3, 5};
    for(i=1; i<3; i++)

There, the type is definite as a class template inside namespace std structured as:

namespace std 
class array;

The elements of such kind of array may have any type T. The second parameter of template states the number of elements the array has throughout its lifespan. Thus, size(), which is a predefined member function always yields 'N'.

Abilities of C++11 Arrays

Arrays have the capability to copy their elements to their in-general static C style array. The elements always contain a definite order. Thus, arrays are considered to be a kind of ordered collection. Arrays provide random accessing of locations. Thus, you can access each of the elements directly in constant time and for that, you just have to know its position. The iterators are random-accessing iterators, so programmers can use any algorithm of the Standard Template Library (Things about STL will be discussed in another tutorial). If programmers have to have a sequence with a rigid number of elements, class array<> will facilitates with the best performance as memory gets allocated on the stack (as and when possible) and reallocation never happens. Also, programmer's can have random access to arrays.

The Move Semantics of Array

Programmers can implement move semantics that will be implicitly provided for arrays. Lets' take an example of code snippet:

std::array<std::string,10> gk1, gk2;
. . . .
gk1 = std::move(gk2);
cout<< gk1;

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram