Code snippet from C++ Standard Library text. This works with sequential containers, why not with associative? I don't see anything contrary in API.. Is iterator to associative container not in the OutputIterator category?
std::set iterators provide only const access to elements.
The reason is simple: they keep their elements in a sorted tree, so changing the value of an element requires the tree to be updated. "Changing" a value must be done via erase + insert.
I ran into this very issue with set last week - apparently, by design (of stl set), the object is the key
that means I can't provide a special operator<() to work with one part of the object and use mutable to allow changes to another part - results in a big const-mess without any benefits
especially since stl set doesn't allow for modification without an erase + insert
Unless a set is a typedef for std::map<T,T>, that definition is wrong.
An associative container stores a relationship between keys and values (e.g. one key to one value, one key to many values, etc.). A set stores "the knowledge of having previously seen a value", so to speak. You can't use a set to, say, store the value of a variable in an interpreted language.
I, too, originally took the meaning of associative to mean storing a relationship between key and value - to associate a key to a value.
However, this strange use of associative container to mean anything that is based on a key appears to be standard - I wonder if it has always been like this, or if it was due to stl's use of this term which bastardized its meaning.