assigning a constiterator to an iterator

You cannot modify a sets elements. Keys must be const to ensure the invariant promised by the set: elements are sorted and unique. A maps elements are sorted too, but you can modify the mapped value of the elements (keys are const as well). Elements of a std::map<A,B> are std::pair<const A,B>.

On cppreference you can read that member aliases for iterators of std::set are

iterator Constant LegacyBidirectionalIterator

const_iterator Constant LegacyBidirectionalIterator

They are both const iterators.

On the other hand for a std::map they are:

iterator LegacyBidirectionalIterator

const_iterator Constant LegacyBidirectionalIterator

Assigning a const_iterator to a non-const one is wrong. Its like trying to cast const away by assigning a pointer to const to a pointer to non-const. It won’t work because you cannot make something that cannot be modified modifiable. That would break const-correctness.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top