Generally when you need to use an array, std::vector is your first choice. It is the most efficient in most cases. If you need an array with a specified length, you can use reserve() to pre-allocate space. , or use a constructor with a length argument (number 3 in the reference). For example:
For fixed-length arrays at compile time, std::array must be used, but I feel that the question is not referring to this, but to the variable-specified array newly introduced in C++ 17 (this feature has been available in C for a long time), and at the same time it Comes with a container std::dynarray for use with, the size is determined during the constructor and cannot then be changed. Now, you can use std::experimental::dynarray, or gsl::dyn_array.
Generally when you need to use an array,
std::vector
is your first choice. It is the most efficient in most cases. If you need an array with a specified length, you can usereserve()
to pre-allocate space. , or use a constructor with a length argument (number 3 in the reference). For example:Of course, there are also template classes
std::array
similar to the built-in compile-time determined length array.C++11 array.
It is also very convenient to specify the number of elements of the vector during initialization.
There is std::array, it is very useful, much easier to use than the built-in array
For fixed-length arrays at compile time, std::array must be used, but I feel that the question is not referring to this, but to the variable-specified array newly introduced in C++ 17 (this feature has been available in C for a long time), and at the same time it Comes with a container std::dynarray for use with, the size is determined during the constructor and cannot then be changed. Now, you can use std::experimental::dynarray, or gsl::dyn_array.