简体   繁体   English

C型数组比std :: array和std :: vector更好的容器,用于从移动语义中受益的固定大小数据?

[英]c-style array better container than std::array and std::vector for fixed size data that benifits from move semantics?

I am creating a new implementation of an N-dimensional vector (we'll call it VectorND ) to replace some old code in a bigger system. 我正在创建N维向量的新实现(我们将其称为VectorND ),以替换较大系统中的一些旧代码。 The vector size will be known at compile time, hence c-style arrays, std::array and std::vector are all viable candidates for the underlying data structure of the VectorND class. 向量大小将在编译时已知,因此c样式数组, std::arraystd::vector都是VectorND类的基础数据结构的可行候选VectorND

I noticed that the vectors get passed around a lot, and think the system would benefit greatly if the new VectorND class could efficiently be moved. 我注意到向量已经大量传递,并且认为如果可以有效地移动新的VectorND类,该系统将受益匪浅。 (I realize that a lot of the 'passing around' won't be able to use move's, but the vectors occur as rvalue's quite often too) (我意识到很多“传递”将无法使用移动,但向量也经常以右值出现)

The std::vector class supports move semantics and as the c-style array is just a pointer both of these allow for trivial a move constructors/assignment operator in the VectorND class. std::vector类支持移动语义,并且由于c样式数组仅是一个指针,因此这两个都允许VectorND类中的琐碎移动构造函数/赋值运算符。 The std::array class however needs O(n) time to be moved, which to me sounds like this "most obvious choice" is in fact the worst in this case. 但是std::array类需要移动O(n)时间,对我来说,这似乎是“最明显的选择”,实际上在这种情况下是最糟糕的选择。

For the remaining two, the c-style array can be allocated as a single chunk of memory and hence really use the fact that we know the size at compile time. 对于其余两个,c样式数组可以分配为单个内存块,因此实际上使用了我们在编译时知道大小的事实。

Am I missing something or is the c-style array indeed the best underlying container for my VectorND class? 我是否缺少某些东西,或者c样式数组确实是我VectorND类的最佳基础容器?

(it sounds strange that the newer stl classes would be a worse choice) (听起来很奇怪,更新的stl类是一个更差的选择)

EDIT: After reading through my post again, I realize that a std::unique_ptr to an std::array might give me the best of both worlds? 编辑:重新阅读我的文章后,我意识到std::unique_ptrstd::array可能会给我两全其美?

I decided to follow juanchopanza's advice and go for the std::vector . 我决定按照juanchopanza的建议去做std::vector As I already mentioned in the question it can be moved (in O(1)) and as juanchopanza pointed out, the overhead is very small. 正如我在问题中已经提到的那样,它可以移动(在O(1)中),并且如juanchopanza所指出的那样,开销很小。

Furthermore I like that it gives me the option to declare the move operations as default, increasing code readability. 此外,我喜欢它为我提供了将移动操作声明为默认操作的选项,从而提高了代码的可读性。 (I wanted to add a virtual destructor so I needed to add them as the compiler won't generate them when a destructor is specified) (我想添加一个虚拟析构函数,所以我需要添加它们,因为在指定析构函数时编译器不会生成它们)

For those interested, the relevant declarations: 对于那些感兴趣的人,相关声明:

/// Copy constructor
VectorND(const VectorND &) = default;
/// Move constructor
VectorND(VectorND &&) = default;
/// Copy assignment
VectorND& operator=(const VectorND &) = default;
/// Move assignment
VectorND& operator=(VectorND &&) = default;
/// Destructor
virtual ~VectorND() = default;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM