c++ - std::array 与数组性能

标签 c++ c++11 stdarray

如果我想构建一个非常简单的数组,例如:

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

我应该改用 std::array 吗?

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

使用 std::array 比普通的有什么优势?它的性能更高吗?只是更容易处理复制/访问?

最佳答案

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

它具有友好的值语义,因此可以按值传递给函数或从函数返回。它的界面可以更方便地查找大小,并与 STL 风格的基于迭代器的算法一起使用。

Is it more performant ?

应该完全一样。根据定义,它是一个简单的聚合,包含一个数组作为其唯一成员。

Just easier to handle for copy/access ?

是的。

关于c++ - std::array 与数组性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30263303/

相关文章:

c++ - 为模板参数的所有组合实例化函数,在运行时选择实例化

c++ - 将可变参数模板类型转换为 void,预期为 ')' 之前

c++ - std::atomic<std::chrono::high_resolution_clock::time_point> 无法编译

c++ - 如何从 std::initializer_list 构建类似 std::array 的数据结构

c++ - 数组元素从未成功添加? (C++)

c++ - 如果他们没有牙套,程序会怎样?

c++ - 将数组数据插入模板

c++ - 在 std::unordered_set 上:请求具有默认散列器的类型列表

c++ - std::accumulate 中的无效转换

c++ - 覆盖标准函数