C++ array class

Introduction

The std::array is a thin wrapper around a C-style array.

Arrays get converted to pointers when used as function arguments, and we should prefer std::array wrapper to old C-style arrays.

The std::array is of the following signature: std::array<type_name, array_size>; A simple example:

#include <iostream> 
#include <array> 

int main() //from   w w w  . j ava  2s  . c om
{ 
    std::array<int, 5> arr = { 1, 2, 3, 4, 5 }; 
    for (auto el : arr) 
    { 
        std::cout << el << '\n'; 
    } 
} 

This example creates an array of 5 elements using a std::array container and prints them out.

Let us emphasizes this once again: prefer std::array or std::vector to old/raw C-style arrays.




PreviousNext

Related