2

What does the value_comp do, like does it check the first value equal to the second?

and * operator before the iterator?

map<char, char> m = 
{
   { 'a', 'A' },
   { 'b', 'B' },
   { 'c', 'C' },
   { 'd', 'D' },
   { 'e', 'E' },
};

auto last = *m.rbegin(); // How does "*" affect the iterator returned
auto i = m.begin();

do 
{

   cout << i->first
      << " = "
      << i->second
      << endl;
} while (m.value_comp()(*i++, last));  
// Does the value_comp compares *i++ and the last value or iterator
JeJo
  • 30,635
  • 6
  • 49
  • 88
tru
  • 55
  • 5
  • Have you read the [documentation](https://en.cppreference.com/w/cpp/container/map)? Could you explain your question more clearly? – Alan Birtles Sep 11 '20 at 10:27

3 Answers3

2

How does "*" affect the iterator returned?

It dereferences the iterator and gives the pointee. Meaning the last has type

std::pair<const char, char>

or

decltype(m)::value_type // or std::map<char, char>::value_type 

If you would have just auto last = m.rbegin(); (i.e. without dereferencing), you would get the iterator there, meaning the last would have deduced to std::map<char, char>::iterator.


Does the value_comp compares *i++ and the last value or iterator?

std::map::value_comp returns:

a function object that compares objects of type std::map::value_type (key-value pairs) by using key_comp to compare the first components of the pairs.

Therefore, it compares the values of the map, not the iterator pointing to the key-value pairs.

JeJo
  • 30,635
  • 6
  • 49
  • 88
1

So, it looks like you have basically two questions:

auto last = m.rbegin(); //How does "" affect the iterator returned

The "*" operator returns the reference of value_type which for your map would be std::pair<const char, char>.

Does the value_comp compares *i++ and the last value or iterator

value_comp() returns the function that compares keys in objects of type value_type. So, in your code it compares keys i->first and last.first. It returns true if the first key is less

rhaport
  • 540
  • 3
  • 11
1

How does "*" affect the iterator returned?

The dereference (*) means that the value stored in last is not the iterator, but a copy of what's stored at that position in the map, which incidentally is the last node of the map. It works much in the same way as dereferencing a pointer, you get the value pointed by it, not the pointer itself.

Does the value_comp compares *i++ and the last value or iterator?

Yes it does. It compares the two keys, the one "pointed by" i and the one stored in last. The method will return true as long as the first argument key is at a lower position on the map than the key of the second argument.

In your example, when the keys match the cycle ends.

As per the description of the method:

It returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.

The comparison object returned is an object of the member type map::value_compare, which is a nested class that uses the internal comparison object to generate the appropriate comparison functional class.

Side note:

It may be clear that *i++ also returns the content of i, and the iterator i is being incremented at each iteration, but this expression can cause confusion, you could use brackets to avoid it, like so *(i++).

anastaciu
  • 23,467
  • 7
  • 28
  • 53