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 usestd::vector
which is more dynamic (Re-sizable)?
dynarray
比vector
更小更简单,因为它不需要管理单独的大小和容量值,也不需要存储分配器。
然而,主要的性能优势在于鼓励实现尽可能在堆栈上分配 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/