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

后端 未结 4 1590
花落未央
花落未央 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:07

    std::set uses the contained values to form a fast data structure (usually, a red-black tree). Changing a value means the whole structure needs to be altered. So, forcing constness, std::set prevents you from pushing it into a non-usable state.

提交回复
热议问题