I am using a set to hold objects. I want to make changes to the first object in the set. This is what I tried:
set<myClass>::iterator i = classlist.begin();
i->value = 0;
This is the error I get:
error: read-only variable is not assignable
How did the variable become read-only? How can it be changed?
This compiles under VS2008:
But this only works by coincidence.
operator<
compares thecomp
members and we change thevalue
member.If you really have to modify the element in
set
, the element has to be once erased from theset
and re-inserted in thatset
.For example:
If
operator<
formyClass
doesn't refer thevalue
member, you can usestd::map
instead ofset
. Assuming a newmyClass
which doesn't havevalue
member, and assuming that the type ofvalue
member isT
, thenstd::map< newMyClass, T >
will meet the purpose.in Visual Studio 2010 when Microsoft updated the STL, they made all iterators constant. So set::iterator and set::const_iterator are the same.
The reason is because in a set, the key is the data itself, and when you change your data, and thus the key, which is a no no.
The microsoft C++ team reported on this breaking change here: http://blogs.msdn.com/b/vcblog/archive/2009/05/25/stl-breaking-changes-in-visual-studio-2010-beta-1.aspx
If you want to be modifying your data, and don't want to be removing and inserting, then use a map instead.
The things in a set are effectively read-only - you cannot change the value of a member of a set in situ. All you can do is remove it, and add a new member with the required new value. This restriction is necessary because of the binary tree implementation of std::set.