C++ STL map container iterator traversal

C++ STL map container iterator traversal

The standard library equips the map container with a bidirectional iterator. This means that map container iterators can only perform ++p, p++, –p, p–, *p operations, and only == or != operators can be used for comparison between iterators.

It is worth mentioning that, compared to sequential containers, the map container provides more member methods (as shown in Table 1). By calling them, we can easily obtain iterators with specified meanings.

Member method Features
begin() Returns a bidirectional iterator pointing to the first (note, the first in sorted order) key-value pair in the container. If the map container is const-qualified, the method returns a bidirectional iterator of const type.
end() Returns a bidirectional iterator pointing to the position after the last element of the container (note that it is the last in sorted order), usually combined with begin(). If the map container is const-qualified, the method returns a bidirectional iterator of const type.
rbegin() Returns a reverse bidirectional iterator pointing to the last (note, the last in order) element. If the map container is const-qualified, the method returns a reverse bidirectional iterator of const type.
render () Returns a reverse bidirectional iterator pointing to the position before the first (note that it is the first in sorted order) element. If the map container is const-qualified, the method returns a reverse bidirectional iterator of const type.
cbegin () It has the same function as begin(), except that it adds a const attribute, which cannot be used to modify the key-value pairs stored in the container.
a few() It has the same function as end(), but on its basis, the const attribute is added, which cannot be used to modify the key-value pairs stored in the container.
crbegin() It has the same function as rbegin(), except that it adds a const attribute, which cannot be used to modify the key-value pairs stored in the container.
crend() It has the same function as rend(), but on the basis of it, the const attribute is added, which cannot be used to modify the key-value pairs stored in the container.
find(key) Find the key-value pair whose key is key in the map container. If it is successfully found, it will return a bidirectional iterator pointing to the key-value pair; otherwise, it will return the same iterator as the end() method. In addition, if the map container is const-qualified, the method returns a bidirectional iterator of const type.
lower_bound(key) Returns a bidirectional iterator pointing to the first key-value pair greater than or equal to key in the current map container. If the map container is const-qualified, the method returns a bidirectional iterator of const type.
upper_bound(key) Returns an iterator pointing to the first key-value pair greater than key in the current map container. If the map container is const-qualified, the method returns a bidirectional iterator of const type.
equal_range(key) This method returns a pair object (contains 2 bidirectional iterators), where the return values ​​of pair.first and lower_bound() methods are equivalent, and the return values ​​of pair.second and upper_bound() methods are equivalent. In other words, this method will return a range whose keys are key-value pairs (map container key-value pairs are unique, so this range contains at most one key-value pair).

Most of the member methods in Table 1, such as begin(), end(), etc., have been used many times when learning sequential containers. Their functions are shown in Figure 2.

Schematic diagram of some member methods of C++ STL map

Traverse
    std::map<std::string, std::string>myMap;
   //调用 begin()/end() 组合,遍历 map 容器
    for (auto iter = myMap.begin(); iter != myMap.end(); ++iter) {
        cout << iter->first << " " << iter->second << endl;//此处用的是迭代器来查找的key和value;
    }

It uses an iterator to find the key and value;
}


Guess you like

Origin blog.csdn.net/weixin_45929885/article/details/113925157