Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector of references

Tags:

c++

stl

vector

I have such problem: I have class Foo, and if have some objects of this class,

Foo a(); 

I need to put this object to 2 different vectors:

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.

like image 220
Mike Minaev Avatar asked May 06 '14 07:05

Mike Minaev


People also ask

Can std::vector hold references?

std::reference_wrapper It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references.

Can you have a vector of references in C++?

It's a flaw in the C++ language. You can't take the address of a reference, since attempting to do so would result in the address of the object being referred to, and thus you can never get a pointer to a reference.

What is an std::vector?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.

How do you pass a vector call by reference?

Use the vector<T> &arr Notation to Pass a Vector by Reference in C++ std::vector is a common way to store arrays in C++, as they provide a dynamic object with multiple built-in functions for manipulating the stored elements.


2 Answers

There are some possibilities:

  1. Store a vector of pointers (use if your vectors share ownership of the pointers):

    std::vector<std::shared_ptr<Foo>> vA, vB; 
  2. Store a vector of wrapped references (use if the vectors do not share ownership of the pointers, and you know the object referenced are valid past the lifetime of the vectors):

    std::vector<std::reference_wrapper<Foo>> vA, vB; 
  3. 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.

  4. Store a vector of std::unique_ptr that wrap the objects (use if your vectors want to handover the ownership of the pointers in which case the lifetime of the referenced objects are governed by the rules of std::unique_ptr class):

    std::vector<std::unique_ptr<Foo>> vA, vB; 
like image 190
utnapistim Avatar answered Sep 19 '22 19:09

utnapistim


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++ reference should explain it nicely:

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() 
like image 41
alpartis Avatar answered Sep 19 '22 19:09

alpartis