mardi 4 octobre 2016

The address of my object in a vector changes

I filled a vector with A objects, then store 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<A> vec;

multimap<const A*, const double > mymultimap;

for (const auto &a : A) {

  double val = a.value();

  vec.push_back(a);

  mymultimap.insert(std::pair<const A*, const double >( &vel.back(), val)); 

cout<<"test1: "<<&vec.back()<<endl;

}


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

[2]

test1: 0x7f6a13ab4000 

test1: 0x7f6a140137c8

test2 :0x7f6a14013000

test2 :0x7f6a140137c8

Aucun commentaire:

Enregistrer un commentaire