Does map :: iterator display lvalues?

In other words, when iis map<K,V>::iterator, follow these steps to provide the expected semantics (that is, it changes the map):

*i = make_pair(k, v);
i->first = k;
i->second = v;

?

Update: The first two lines are unacceptable, because the return value operator*is (converted into?) A pair<const K, V>. What about the third line?

Assuming the answer is yes to three, this will mean that:

  • Items map<K,V>are saved as pair<K,V>somewhere
  • Or there is some kind of smart proxy class that returns map<K,V>::iterator::operator*. In this case, how is it implemented operator->?
+3
source share
4 answers

:

  • a map<Key,T> value_type pair<const Key,T> 23.3.1/2

  • , 23.3.1/1

  • , 24.1.4/1

  • a value_type T *a T & ( " T", ) ( 74 24.1.3)

- , - .

+4

. , , , node pair<const K,V>. , pair<const K, V>, first.

* pair<const K, V>& -, ( ). , operator ->.

+3

-, * l. lvalue C , () (). ++ - lvalues. , , * lvalue. lvalue, , .. &*i, &i->first &i->second ( , &).

-, , l. , lvalue . , lvalue. , , value_type std::map. , const-, . , . , .

, lvalue. lvalue , . lvalue.

As for your assumption about saving elements std::map, I would say that in a typical implementation they will be stored as objects pair<const K, V>, that is, exactly what the dereference operator evaluates. As a rule, the card does not need to change the key part of the pair after its initialization, therefore, it should not encounter any problems with the fact that the first member of the pair is const-qualified.

+1
source
map<K,V>::iterator i = my_map.begin();

*i = make_pair(k, v); // invalid, you cannot assign to a pair<const K,V>&
i->first = k; // invalid cannot write to const
i->second = v; // valid
+1
source

Source: https://habr.com/ru/post/1765839/


All Articles