I tried doing this:
std::set< pair<int, int> > mySet;
// fill the set with something
mySet.find( make_pair(someValueX, someValueY) )->first = newX;
But I get the following error on compilation:
error: assignment of member 'std::pair<int, int>::first' in read-only object|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===||
You can't, you need to remove the pair and insert the new one. This has nothing to do with std::pair , it's a limitation imposed by std::set .
To modify them, one can only delete and add components. C++ pair is a type that is specified under the utility> header and is used to connect two pair values. The pair's values can be of separate or identical types. To view the values in a pair independently, the class has the member functions first() and second().
In Set duplicate values are not allowed to get stored. On other hand in case of MultiSet we can store duplicate values. In case of Set, one cannot change the value once it gets inserted however we can delete or insert it again. However in case of MultiSet also we cannot change the value once get inserted.
Members of std::set
are const
, because changing them could make their ordering invalid. You would have to erase the pair and re-insert it after it was changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With