简体   繁体   English

C ++:在向量中插入什么 - 指针或引用,向量何时复制它的元素?

[英]C++: What to insert in vectors - pointers or references, when does a vector copy it's elements?

Suppose I have complex objects, like structs containing a huge amount of members - and I want to store them in a vector. 假设我有复杂的对象,比如包含大量成员的结构 - 我想将它们存储在向量中。

Now vector::push_back(...) works over call-by-reference (rather than call-by-value), so in the first moment the passed object is not copied. 现在,vector :: push_back(...)通过引用调用(而不是按值调用)工作,因此在第一时间不会复制传递的对象。 But what about later? 但是后来呢? Does a vector internally store pointers or direct references? 向量内部存储指针或直接引用吗? When the vector needs to expand, are the contained elements themselves copied or their addresses? 当向量需要扩展时,包含的元素本身是复制还是它们的地址?

This finally results in the question, if - for large objects - the objects themselves should be stored in a vector or rather pointers to these objects. 这最终导致了一个问题,如果 - 对于大型对象 - 对象本身应该存储在向量中,或者指向这些对象的指针。 Is there a kind of best practice for that? 那有什么最好的做法吗?

vector::push_back(...) works over call-by-reference (rather than call-by-value), so in the first moment the passed object is not copied vector::push_back(...)通过引用调用(而不是按值调用)工作,因此在第一时间不传递传递的对象

vector::push_back() stores the copy of the object being added to it. vector::push_back()存储要添加到其中的对象的副本。

Does a vector internally store pointers or direct references? 向量内部存储指针或直接引用吗?

How a vector internally stores elements is an implementation detail but the complexity and the behavior requirements leave little room for variation. 向量内部存储元素的方式是实现细节,但复杂性和行为要求几乎没有变化的余地。 Vector stores the elements in a contiguous memory locations much like arrays. Vector将元素存储在连续的内存位置,就像数组一样。

When the vector needs to expand, are the contained elements themselves copied or their addresses? 当向量需要扩展时,包含的元素本身是复制还是它们的地址?

When a vector expands, it needs to copy all the objects to a new contiguous memory, again this is a implementation detail. 当向量扩展时,它需要将所有对象复制到新的连续内存,这也是一个实现细节。 If elements are pointers then the pointers themselves will be copied. 如果元素是指针,则指针本身将被复制。

The objects themselves should be stored in a vector or rather pointers to these objects. 对象本身应存储在向量中,或者指向这些对象的指针。 Is there a kind of best practice for that? 那有什么最好的做法吗?

If your objects are bigger and you do not want to place them in a vector then you can store pointers to them in the vector but then do not store raw pointers in vector, use a suitable smart pointer as per your requirement. 如果您的对象较大并且您不想将它们放在向量中,那么您可以在向量中存储指针,但是不要将原始指针存储在向量中,根据您的要求使用合适的智能指针 It ensures perfect RAII and you do not need to be bothered about the memory management explcitily. 它确保完美的RAII ,您无需担心存储器管理。

std::vector::push_back saves copy of the object. std::vector::push_back保存对象的副本

If you need performance by avoiding copy, then you could use vector of pointers. 如果你通过避免复制需要性能,那么你可以使用指针向量。

std::vector<BigObject*> objects; //manage memory yourself

Or you can use some sort of smart pointer, so as to avoid managing memory yourself. 或者您可以使用某种智能指针,以避免自己管理内存。 For example, you could use std::unique_ptr if your compiler supports it: 例如,如果编译器支持std::unique_ptr则可以使用它:

std::vector<std::unique_ptr<BigObject>> objects;

If you use pointers (or smart pointers), then copying is still there, but this time, the copy is done for pointers (or smart pointers) which is much cheaper. 如果你使用指针(或智能指针),那么复制仍然存在,但这一次,复制是针对指针(或智能指针)完成的,这要便宜得多。

From @Loki's comment: 来自@ Loki的评论:

Or you can use boost::ptr_vector<> which is designed to hold pointers and will return references when accesses thus makeing it easy to use standard algorithms as they all expect objects. 或者您可以使用boost::ptr_vector<>来设计保存指针,并在访问时返回引用,从而使标准算法变得容易,因为它们都期望对象。

Actually, if you're iterating over the vector more than you're adding objects to it, you will greatly outperform pointer by adding objects, as they will be contiguous in memory. 实际上,如果你在向量上迭代而不是向它添加对象,那么通过添加对象将大大超过指针,因为它们在内存中是连续的。

However if you're doing more adding and erasing to and from the vector, than you are iterating over it, pointers will be more efficient. 但是,如果你正在向向量和向量进行更多的添加和擦除,那么与迭代它相比,指针将更有效。

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

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