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