unordered_set non const iterator

后端 未结 5 1305
野趣味
野趣味 2021-01-04 23:39

For testing purposes I created a little unordered_set and tried to iterate over the set. The set holds an own class:

class Student {
private:
    int matrNr;
         


        
5条回答
  •  长情又很酷
    2021-01-05 00:15

    They value type of a set is const K, and for a map it is pair; ditto for the unordered versions.

    An iterator gives you access to value_type &, and a const-iterator to a const value_type &. As you can see, neither iterator type can "undo" the constness of the key.

    The reason the key is immutable is that it forms an integral part of the underlying data structure; changing the key would require a non-trivial internal rearrangement which would cause all sorts of problems (e.g. non-zero computational complexity (for element access!), and confused iterator ordering).

提交回复
热议问题