std :: dynarray vs std :: vector [英] std::dynarray vs std::vector
问题描述
C ++ 14提供 std :: dynarray
:
C++14 presents std::dynarray
:
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
must be allocated in run-time as same as std::vector
.
std :: dynarray
的好处和用法,而我们可以使用 std :: vector
也可以改变大小?
So what are the benefits and the usage of std::dynarray
while we can use std::vector
which is more dynamic (and also re-sizable)?
推荐答案
$ c> 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
小于和简化向量
,因为它不需要管理单独的大小和容量值,并且不需要存储分配器。
dynarray
is smaller and simpler than vector
, because it doesn't need to manage separate size and capacity values, and it doesn't need to store an allocator.
但是主要的性能优势来自于这样的事实,即尽可能地在堆栈上分配 dynarray
,避免任何堆分配。例如
However the main performance benefit is intended to come from the fact that implementations are encouraged to allocate dynarray
on the stack when possible, avoiding any heap allocation. e.g.
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 ++委员会会议上,决定从C ++ 14中提取 std :: dynarray
,并发出一个单独的数组扩展TS(技术规范)文档定义 std :: experimental :: dynarray
和运行时绑定的数组(ARB,类似于C99 VLA。)这意味着 std :: dynarray
几乎肯定不会在C ++ 14中。
This optimisation requires cooperation from the compiler, it can't be implemented as a pure library type, and the necessary compiler magic has not been implemented and noone is sure how easy it is to do. Because of the lack of implementation experience, at the C++ committee meeting in Chicago last week it was decided to pull std::dynarray
from C++14 and to issue a separate array extensions TS (technical specification) document defining std::experimental::dynarray
and arrays of runtime bound (ARBs, similar to C99 VLAs.) This means std::dynarray
will almost certainly not be in C++14.
这篇关于std :: dynarray vs std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!