It’s been a long time since we used STL so anyhelp would be appreciated. Not sure what we’re doing wrong here…
Given this, why does this code throw an error:
“you cannot assign to a variable that is const”
struct person
{
int age;
bool verified;
string name
bool operator< (person const &p)
{
return (age < p.age);
}
};
multiset<person> msPerson;
multiset<person>::iterator pIt;
// add some persons
while (adding people)
{
Person p;
p.name=getNextName();
p.age=getNextAge();
msPerson.insert(p);
}
pIt = msPerson.begin();
// try to verify
pIt->verified = true; <---- **error here....**
They return a const iterator if the container is ordered. The idea is that if you change the content the container doesn’t know and it cannot guarantee order. Vector does not, but map does.
If you’re sure your update does not affect the sort order you can cast away the const-ness. As always proceed with caution if you do that.