Tuesday, 1 May 2018

c++ - The address of my object in a vector changes

I filled a vector with A objects, then stored these objects address in a multimap [1], but the print message shows that the reference to the object stored in the vector changed [2]. Do you see why? and how avoid any changes.



//[1]
vector vec;
multimap mymultimap;

for (const auto &a : A) {
double val = a.value();

vec.push_back(a);
mymultimap.insert(std::pair( &vel.back(), val));

// displaying addresses while storing them
cout<<"test1: "<<&vec.back()<
}

//[2]
// displaying addresses after storing them

for(auto &i : vec)
cout << "test2: " << &i <


Results:



test1: 0x7f6a13ab4000  
test1: 0x7f6a140137c8
test2 :0x7f6a14013000
test2 :0x7f6a140137c8

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...