c++ - std::dynarray 与 std::vector

标签 c++ stdvector

C++14 呈现 std::dynarray :

std::dynarray is a sequence container that encapsulates arrays with a size that is fixed at construction and does not change throughout the lifetime of the object.

std::dynarray 必须和 std::vector 一样在运行时分配。

那么 std::dynarray 有什么好处和用途,而我们可以使用更动态(也可以重新调整大小)的 std::vector

最佳答案

So what are the benefits and the usage of std::dynarray, when we can use std::vector which is more dynamic (Re-sizable)?

dynarrayvector 更小更简单,因为它不需要管理单独的大小和容量值,也不需要存储分配器。

然而,主要的性能优势在于鼓励实现尽可能在堆栈上分配 dynarray,避免任何堆分配。例如

std::dynarray<int> d(5);   // can use stack memory for elements
auto p = new std::dynarray<int>(6);  // must use heap memory for elements

这种优化需要编译器的配合,它不能作为纯库类型来实现,而且必要的编译器魔法还没有实现,没有人知道它是多么容易做到。由于缺乏实现经验,上周在芝加哥举行的 C++ 委员会 session 上,决定从 C++14 中拉出 std::dynarray 并发布单独的数组扩展 TS(技术规范)定义 std::experimental::dynarray 和运行时绑定(bind)数组(ARB,类似于 C99 VLA)的文档。这意味着 std::dynarray 几乎肯定不会出现在 C 中++14.

关于c++ - std::dynarray 与 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19111028/

相关文章:

c++ - 通用数据库管理器(包装器)

c++ - sqlite3 c/c++,获取聚合查询涉及的表名

c++ - 为什么要在std::vector::push_back(T object)方法中构造对象时调用析构函数?

c++ - 使用 range-for 循环以相反的顺序迭代 vector ?

c++ - 如何将变量从类元素传递到类元素? (C++)

c++ - SFML 和移动对象到位置

c++ - 在匿名类中返回* this时, 'auto'返回类型是什么类型?

c++ - 如何在C++中创建元组 vector ?

c++ - 就地构造函数 std::vector

c++ - 如何检查 C++ vector 是否已包含具有特定值的元素(在本例中为结构)?