What does iterator->second mean?

后端 未结 2 2025
[愿得一人]
[愿得一人] 2020-12-22 16:59

In C++, what is the type of a std::map<>::iterator?

We know that an object it of type std::map::iterator has

2条回答
  •  被撕碎了的回忆
    2020-12-22 17:39

    The type of the elements of an std::map (which is also the type of an expression obtained by dereferencing an iterator of that map) whose key is K and value is V is std::pair - the key is const to prevent you from interfering with the internal sorting of map values.

    std::pair<> has two members named first and second (see here), with quite an intuitive meaning. Thus, given an iterator i to a certain map, the expression:

    i->first
    

    Which is equivalent to:

    (*i).first
    

    Refers to the first (const) element of the pair object pointed to by the iterator - i.e. it refers to a key in the map. Instead, the expression:

    i->second
    

    Which is equivalent to:

    (*i).second
    

    Refers to the second element of the pair - i.e. to the corresponding value in the map.

提交回复
热议问题