【C++STL】map

insert image description here

1. Introduction to map

  1. Map is an associative container, which stores elements composed of key value key and value value in a specific order (compared by key)
  2. In a map, the key-value key is usually used to sort and uniquely identify elements, and the value associated with this key-value key is stored. The types of the key value key and the value value may be different, and inside the map, the key and value are bound together through the member type value_type, which is aliased as pair typedef
    pair<const key,T>value_type;
  3. Internally, elements in a map are always compared and sorted by key
  4. The speed of accessing a single element through the key value in the map is slower than that of the unordered_map container, but the map allows direct iteration of the elements according to the order (that is, when iterating the elements in the map, an ordered sequence can be obtained)
  5. map supports subscript accessors, that is, put the key in [], and you can find the corresponding value
  6. Maps are usually implemented as binary search trees (more precisely, balanced binary search trees (red-black trees)).

2. The use of map

What is stored in the map is pair
T1 first is the key value, and T2 second is the value value

template <class T1, class T2>
struct pair
{
    
    
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(): first(T1()), second(T2())
{
    
    }
pair(const T1& a, const T2& b): first(a), second(b)
{
    
    }
};

insert插入
insert image description here
Let's test the insertion of the map.
insert image description here
First, the map stores the data type of pair. We can use the anonymous construction of pair<string,string>, but this way of writing is more complicated.
C++ provides a make_pair functor.
insert image description here
The advantage of using the make_pair functor is that it is easier to write, and the other is that it automatically deduces the type, so there is no need to specify the type like the anonymous construction of pair.

Secondly, the map's iterator dereference returns a pair structure, which cannot be directly output, and its internal properties need to be specified.
insert image description here

However, map is overloaded with ->, you can use -> output directly. Both have the same effect
insert image description here


operator[]重载

If we want to count the number of fruits, we can count like this
insert image description here

We can also use map的operator[]overloading to complete the requirements.
insert image description here
Let’s analyze it. operator[]
insert image description here
insert image description here
Calling operator[] is actually calling this big thing. We split it and the
insert image description here
return value of make_pair is a pair, but we see that the return of insert is also used here. Value
Next, let’s explain what insert的返回值
insert image description here
insert inserts value_type其实就是pair, and the return value is also a pair, but for this pair 第一个参数是一个迭代器, 第二个参数是一个bool值
according to the description in the literature, if the inserted element does not exist in the map, it will be inserted. 返回的迭代器指向该元素位置If 已存在, the returned iterator points to 该元素在map中的位置; the second A bool value, if the element is newly inserted, then 返回真, if the element already exists, 返回假.
So ( this->insert( make_pair( k,mapped_type() ) ) ) is actually a pair<iterator,bool>
first that takes the pair again, which is the iterator, then dereferences the pointed pair, and finally takes the second attribute, which is the value. and返回该value的引用

Therefore, if the fruit does not exist, it will be inserted. Because the value is an int, the default structure will be adjusted, initialized to 0, and then the reference of the value will be returned, and ++ will become 1. If the fruit exists, it will not be inserted, but the value will still be
returned The reference, ++ makes the value of value bigger.

So operator[] has four functions

  1. insert
  2. insert+modify
  3. Revise
  4. look up

insert image description here
dict["left"] only specifies the key, then value needs to call the default construction of string
dict["right"]="right", the same as above, but [] returns value, we change it to "right"
dict[" string"]="(string)", the previous part returns the reference of value, we modify it
because [] will return value, so it can also be searched.

conclusion

thanks for reading

If you think this article is helpful to you, you might as well like it to support the blogger, please, this is really important to me.
insert image description here

Guess you like

Origin blog.csdn.net/m0_72563041/article/details/130374571