mercredi 26 avril 2017

why std::set in function from

I have 10000 non-const objects in std::vector. I want to put them in the std::set.

set<Object,bool(*)(Object, Object)> s([](Object a, Object b){
                                     return a.getId() < b.getId()});

I want to change some variables in this objects.

for_each(s.begin(), s.end(), [=](Object& a){
    a.setId(new_id);
  });

Why when I'm using set in functions from , it gives me the error that object "would lose const qualifier". But with vector everything is normal. I have a couple of thoughts: 1) in for_each it's automatically used const_iterator. 2) the set just cast non-const to const, but i think it's wrong because i can without problem gets with iterator an object and change it.

Can someone explain me, what happens here

Aucun commentaire:

Enregistrer un commentaire