Tuesday, May 1, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...