lundi 31 juillet 2017

Shallow copy of pointers from a map to a vector

I have

map<uint8_t *,MyObject *, lex_compare> my_map;
vector<MyObject *> my_vector;

If I do:

for(auto &pair_el : my_map)
    (pair_el.second)->print();

where print() is a function of the object pointed everything is alright. But I want copy the pointer of map in my_vector (that is empty) (in a shallow way).And I do:

int i=0;
for(auto &pair_el : my_map)
{
    my_vector.push_back(pair_el.second);
    (pair_el.second)->print();
    my_vector[i]->print();
    ++i;
}

but my_vector[i]->print(); goes ,later some execution, in segmentation fault. What could be your problem?

Aucun commentaire:

Enregistrer un commentaire