There are two ways in which I can easily make a key,value attribution in C++ STL: maps and sets of pairs. For instance, I might have
map<key_class,value_class>
or
set<pair<key_class,value_class> >
In terms of algorithm complexity and coding style, what are the differences between these usages?
Set elements cannot be modified while they are in the set.
set‘siteratorandconst_iteratorare equivalent. Therefore, withset<pair<key_class,value_class> >, you cannot modify thevalue_classin-place. You must remove the old value from the set and add the new value. However, ifvalue_classis a pointer, this doesn’t prevent you from modifying the object it points to.With
map<key_class,value_class>, you can modify thevalue_classin-place, assuming you have a non-const reference to the map.