我正在codedope.com上学习C ++,并且已经在另一个网站Learncpp.com上阅读了一份文档。但是这两个网站有不同的方法将值分配给数组。
//codesdope.com
std::array<int,5> n {{1,2,3,4,5}};
Run Code Online (Sandbox Code Playgroud)
//learncpp.com
std::array<int,5> n = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)
哪种方法更准确?我应该选择哪种方式?它们之间有什么区别?
在CWG 1270之前的C ++ 11中需要双括号(修订后的C ++ 11中以及C ++ 14及更高版本中不需要双括号):
// construction uses aggregate initialization
std::array<int, 5> a{ {1, 2, 3, 4, 5} }; // double-braces required in C++11 prior to the CWG 1270 revision
std::array<int, 5> a{1, 2, 3, 4, 5}; // not needed in C++11 after the revision and in C++14 and beyond
std::array<int, 5> a = {1, 2, 3, 4, 5}; // never required after =
Run Code Online (Sandbox Code Playgroud)