What does iterator->second mean?
Asked Answered
S

3

193

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

We know that an object it of type std::map<A,B>::iterator has an overloaded operator -> which returns a std::pair<A,B>*, and that the std::pair<> has a first and second member.

But, what do these two members correspond to, and why do we have to access the value stored in the map as it->second?

Sprinkling answered 16/3, 2013 at 15:54 Comment(1)
A std::map stores a key and a value.map::iterator.second refers to the value.Pickford
R
313

I'm sure you know that a std::vector<X> stores a whole bunch of X objects, right? But if you have a std::map<X, Y>, what it actually stores is a whole bunch of std::pair<const X, Y>s. That's exactly what a map is - it pairs together the keys and the associated values.

When you iterate over a std::map, you're iterating over all of these std::pairs. When you dereference one of these iterators, you get a std::pair containing the key and its associated value.

std::map<std::string, int> m = /* fill it */;
auto it = m.begin();

Here, if you now do *it, you will get the the std::pair for the first element in the map.

Now the type std::pair gives you access to its elements through two members: first and second. So if you have a std::pair<X, Y> called p, p.first is an X object and p.second is a Y object.

So now you know that dereferencing a std::map iterator gives you a std::pair, you can then access its elements with first and second. For example, (*it).first will give you the key and (*it).second will give you the value. These are equivalent to it->first and it->second.

Ranking answered 16/3, 2013 at 16:4 Comment(4)
Why don't they just use [0] and [1] (for "first" and "second") like everything else in programming?Almanza
@AdamCross Because operator[] has to return a specific type but first and second can have different types. On the other hand, std::tuple has a special helper function std::get for accessing its elements by index.Ranking
Supplements of official documents: map, under Member types, it has the value_type, which refers to pair, clearly you will see its member variables first and second.Adlai
Why didn't they just call it key and val or even k and v which is far more logical and easy to deduce rather than something as esoteric as first and second?Indemonstrable
H
24

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<const K, V> - 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.

Hammertoe answered 16/3, 2013 at 16:0 Comment(2)
The words "key" and "value" would have been more intuitive than "first" and "second", which imply ordering.Martyrdom
@Martyrdom lets overload it :DFiddlefaddle
R
2

Used for map and unordered map.

map stores the key-value pair where i->first is for key and i->second is reflecting value.

#include<bits/stdc++.h>
#define long long int
using namespace std;
int32_t main(){
  map<int,int> m;
  m.insert({1,2});
  m.insert({2,4});
  for(auto i:m){
      cout<<"key - "<<i.first<<" "<<" Value - "<<i.second<<endl;
  }
}
Roundworm answered 16/4, 2021 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.