Why does std::set seem to force the use of a const_iterator?

后端 未结 4 1582
花落未央
花落未央 2020-12-18 17:51

Consider the simple program below, which attempts to iterate through the values of a set using NON-const references to the elements in it:

#include 

        
4条回答
  •  天涯浪人
    2020-12-18 18:06

    A set is like a map with no values, only keys. Since those keys are used for a tree that accelerates operations on the set, they cannot change. Thus all elements must be const to keep the constraints of the underlying tree from being broken.

提交回复
热议问题