0

Suppose I have a std::vector<T> v, and a T t, is it possible for me to add t to v while maintaining reference to the vector element its assigned to? I know it's probably unsafe to do this, but I just want to know to be honest. An example of what I'm achieving is below:

std::vector<int> v;
int t = 1;
v.push_back(t);//replace with magic method
// now v[0] = t;
t = 2;
std::cout << v[0] << std::endl; // OUT: 2, instead of 1 because t, i.e. v[0] changed.
Lnio Yarschov
  • 23
  • 1
  • 2
  • A vector(or other containers) can't hold references. Use a vector of pointers. [Why can't I make a vector of references?](https://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references) – Jason Mar 30 '23 at 06:56

0 Answers0