C++ std::引用向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23488326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::vector of references
提问by Mike Minaev
I have such problem: I have class Foo
, and if have some objects of this class,
我有这样的问题:我有 class Foo
,如果有这个类的一些对象,
Foo a();
I need to put this object to 2 different vectors:
我需要把这个对象放到 2 个不同的向量上:
std::vector<Foo> vA, vB;
and if a
changes in vA
it should be changed in vB
, vectors vA
and vB
can be different, but they can have same objects. I know that it is possible to do with Boost, but I can't use Boost.
如果它的a
变化vA
应该在vB
,向量中改变,vA
并且vB
可以不同,但它们可以具有相同的对象。我知道可以使用 Boost,但我不能使用 Boost。
回答by utnapistim
There are some possibilities:
有一些可能性:
Store a vector of pointers (use if your vectors share ownershipof the pointers):
std::vector<std::shared_ptr<Foo>> vA, vB;
Store a vector of wrapped references (use if the vectors do not share ownershipof the pointers, and you know the object referenced arevalid past the lifetime of the vectors):
std::vector<std::reference_wrapper<Foo>> vA, vB;
Store a vector of raw pointers (use if your vectors do not share ownership of the pointers, and/or the pointers stored may change depending on other factors):
std::vector<Foo*> vA, vB;
This is common for observation, keeping track of allocations, etc. The usual caveats for raw pointers apply: Do not use the pointers to access the objects after the end of their life time.
Store a vector of
std::unique_ptr
that wrap the objects (use if your vectors want to handover the ownership of the pointersin which case the lifetime of the referenced objects are governed by the rules ofstd::unique_ptr
class):std::vector<std::unique_ptr<Foo>> vA, vB;
存储指针向量(如果您的向量共享指针的所有权,请使用):
std::vector<std::shared_ptr<Foo>> vA, vB;
存储包装引用的向量(如果向量不共享指针的所有权,并且您知道引用的对象在向量的生命周期之后仍然有效,则使用):
std::vector<std::reference_wrapper<Foo>> vA, vB;
存储原始指针的向量(如果您的向量不共享指针的所有权,和/或存储的指针可能会根据其他因素发生变化,请使用):
std::vector<Foo*> vA, vB;
这对于观察、跟踪分配等很常见。原始指针的常见警告适用:不要在对象生命周期结束后使用指针访问对象。
存储一个
std::unique_ptr
包装对象的向量(如果你的向量想要移交指针的所有权,在这种情况下被引用对象的生命周期由std::unique_ptr
类规则控制):std::vector<std::unique_ptr<Foo>> vA, vB;
回答by alpartis
You need a vector of references. And since you specify that you need to use std::vector, then the correct thing to do is wrap your objects in the std::reference_wrapper
. This page from C++ referenceshould explain it nicely:
您需要一个引用向量。并且由于您指定需要使用 std::vector,因此正确的做法是将对象包装在std::reference_wrapper
. 这个来自 C++ 参考的页面应该很好地解释了它:
vector<reference_wrapper<Foo>> vA, vB;
vA.push_back(a);
vB.push_back(a); // puts 'a' into both vectors
// make changes to 'a'
// use .get() to get the referenced object in the elements of your vector
// vA[0].get() == vB[0].get()
回答by Dr. Debasish Jana
Instead of keeping Foo as object keep as pointer to object Foo, e.g.
而不是将 Foo 作为对象保持作为指向对象 Foo 的指针,例如
std::vector<Foo *> vA, vB;
and manage allocations and deallocations carefully to avoid memory leakage (Allocating but not deallocating when done). If you allocate one Foo object and keep the pointer in both vA and vB , then you are keeping essentially same object in both through the pointer. Otherwise you may use smart pointer (better) refer:
并仔细管理分配和释放以避免内存泄漏(完成时分配但不释放)。如果您分配一个 Foo 对象并将指针保留在 vA 和 vB 中,那么您通过指针在两者中保留了本质上相同的对象。否则你可以使用智能指针(更好)参考: