c++arraysc++11stdarray

What are the advantages of using std::array over C-style arrays?


If I want to build a very simple array like:

int myArray[3] = {1, 2, 3};

Should I use std::array instead?

std::array<int, 3> a = {{1, 2, 3}};

What are the advantages of using std::array over usual ones? Is it more performant? Just easier to handle for copy/access?


Solution

  • What are the advantages of using std::array over usual ones?

    It has friendly value semantics, so that it can be passed to or returned from functions by value. Its interface makes it more convenient to find the size, and use with STL-style iterator-based algorithms.

    Is it more performant ?

    It should be exactly the same. By definition, it's a simple aggregate containing an array as its only member.

    Just easier to handle for copy/access ?

    Yes.