原始数组是否比 std::array 有任何优势?
Does a raw array have any advantages over a std::array?
根据 this question about raw arrays vs std::vector 上已接受的答案,原始数组(早在 2010 年)的优点是:
- arrays are slightly more compact: the size is implicit
- arrays are non-resizable; sometimes this is desireable
- arrays don't require parsing extra STL headers (compile time)
- it can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using)
- fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed
据我所知,std::array 解决了除第三点以外的所有问题。
所以除非我迫切需要改进我的编译时间,否则是否有任何理由在 C++11 中使用原始数组而不是 std::array?
是的,它不需要您明确指定大小,这样更容易手动初始化它:
char const *messages[] =
{
"Hi",
"Bye",
"foo",
"bar"
};
根据 this question about raw arrays vs std::vector 上已接受的答案,原始数组(早在 2010 年)的优点是:
- arrays are slightly more compact: the size is implicit
- arrays are non-resizable; sometimes this is desireable
- arrays don't require parsing extra STL headers (compile time)
- it can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using)
- fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed
据我所知,std::array 解决了除第三点以外的所有问题。
所以除非我迫切需要改进我的编译时间,否则是否有任何理由在 C++11 中使用原始数组而不是 std::array?
是的,它不需要您明确指定大小,这样更容易手动初始化它:
char const *messages[] =
{
"Hi",
"Bye",
"foo",
"bar"
};