C ++ Standard Library map type

Source: Internet
Author: User

Map is a set of key-value pairs. The map type can generally be understood as an associated array: You can use the key as the subscript to obtain a value, just like the built-in array. The essence of association is that the element value is associated with a specific key, rather than obtained by the position of the element in the array.

1. Definition of map object
To use a map object, the map header file must be included. When defining a map object, you must specify the key and value type respectively.

Map <string, int> word_count;

String is the key, int Is the value

Map Constructor

Map <k, v> m; // create an empty map object named m. The key and value types are k and v.

Map <k, v> m (m2); // create a copy of m2. m and m2 must have the same key type and value type.

Map <k, v> m (B, e) // create a map-type object m, store copies of all elements in the range of iterator B and e. The element type must be converted to pair <const k, v>.

2. map-defined types
Map <k, v >:: key_type is the type of the key used as the index in the map container.

Map <k, v >:: maped_type in the map container, the value type associated with the key

Map <k, v >:: value_type is a pair type. Its first element has the const map <k, v >:: key_type type, while the second element is map <k, v>: maped_type.

When the map iterator is unreferenced, a pair object is obtained. Its first member stores the key as const, while the second member stores the value.

3. add elements to map
Like other subscript operators, the subscript of map uses indexes to obtain the value associated with the key. If the key is in the container, the subscript operation of map is the same as that of the subscript element of vector: return the value associated with the key. The map container creates a new element for the key and inserts it into the map object only when the queried key does not exist. At this time, the associated values are initialized with values: the elements of the class type are initialized with the default constructor, and the elements of the built-in type are initialized to 0.

 

Insert operation provided by map container

M. insert (e) e is the value_type value on m. If the key (e. first) is not in m, a new element of e. second is inserted. If the key already exists in m, m remains unchanged. This function returns a pair type object, which contains the map iterator pointing to the element of e. first and a bool type object, indicating whether the element is inserted.

The elements between the m. insert (beg, end) iterators must be value_type key-value pairs. Element in this range. If it is not in m, it is inserted.

M. insert (iter, e) is searched for from the iterator iter. If m does not exist, insert it. Return the iterator, pointing to the element with the specified key in m.

 

Word_count.insert (map <string, int>: value_type ("Anna", 1 ));

It can also be simplified to word_count (make_pair ("Anna", 1 ));

 

Use the first method to insert the returned pair type.

Pair <map <string, int >:: iterator, bool> ret = word_count.insert (map <string, int >:: value_type ("Anna", 1 ));

4. Search for and obtain elements in map
Use a subscript to obtain the value of map China. If the key does not exist, the seemingly element is inserted. The following map provides two operations:

M. count (k) returns the number of times k appears in m.

If m. find (k) exists, the iterator pointing to the element is returned. Otherwise, the iterator exceeding the end is returned.

For map, the return value of count can only be 0 or 1. You can use this function to determine whether the returned value exists. Then, you can use the subscript operation to insert new elements if the returned value does not exist.

For the find function, determine whether it exists by determining whether it is equal to the iterator that exceeds the end. If it exists, use the returned iterator. The returned iterator is of the pair type, use the following method to obtain the value int value = iter-> second;

5. Delete elements from map
M. erase (k) deletes the elements whose key is k. Returns the size_type value, indicating the number of elements to be deleted.

M. erase (p) removes the element pointed to by iterator p from m. P must point to an existing element in m and cannot be equal to m. end (). Return void type

M. erase (B, e) removes a range of elements from m, which are marked by the iterator for B and e. B and e must mark a valid range in m: Both B and e must point to the element in m or the next position of the last element. In addition, B and e are either equal (the deleted range is blank at this time), or the elements pointed to by B must appear before the elements pointed to by e. Returns the void type.

 

In addition, map also provides a special erase operation. The parameter is a key_type value. If the key that owns the element exists, the element is deleted and the number of deleted items is returned. For map, the returned value is 1 or 0. 0 indicates that the deleted element does not exist in the map.

 

For map object traversal, you can use the iterator, m. end () and m. begin ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.