C + + Map

Source: Internet
Author: User

Before you learn map, you need to know the pair type

I. What is a map

Map is a collection of key-value pairs. The map type is usually understood as an associative array: You can use the key as the subscript to get a value, just like the built-in array type. The essence of the association is that the value of the element is associated with a particular key, not by the position of the element in the array.


Two. Definition of Map Object

1) to include the map header file.

#include <map>

2) When defining a map object, you must indicate the type of the key and the value separately

Map<string, int> A;

The preceding statement defines a map object named A, with the key string type and the associated value Int.

3) Map Constructor

* map<k, v> m; Creates an empty map object with keys and values of K and V, respectively

* Map<k, v> m (m2); Create a copy of M2 m, m and M2 must have the same key value type

* Map<k,v> m (b, E); Create a copy of all elements in the scope of the m, storage iterator B and e tags, the element type must be convertible to Pair<const K, v>

4) Constraint of key type

The key type must define the < operator, and the operator will work correctly.

For example

map< vector<int>::iterator, int > B; The right definition

map< List<int>::iterator, int> C; The error is defined because the List<int>::iterator type does not support the < operator.


Third, the type defined by the map class

Map<k, v>:: Key_type type of key

Map<k, v>:: Mapped_type The type of the value associated with the key

Map<k, v>:: Value_type A pair type whose first element has a const map<k,v>::key_type type, while the second element is Map<k,v>::mappe The D_type type.

For example, the map<string is defined above, the Value_type type of int> A is Pair<const string, and the int> type

Keep in mind that the Value_type type is pair type.


Four. Adding elements to a map

There are two ways: 1. Pass the subscript operator 2. Insert function

1) by subscript operator

For example

Map <string, int> Word_count;

Word_count ["Anna"] = 1;

The above statement will take place something:

1. Find the element in Word_count that has the key for Anna.

2. Insert a new key-value pair into the Word_count. Its key is the const string object that holds Anna. And its value is initialized, in this case it is initialized to 0.

3. Insert the new key-value pair into the Word_count.

4. Read the newly inserted element and assign its value to 1.


Using subscript to access a nonexistent element causes a new element to be added to the map, and its key is the subscript value.


2) Use of Map::insert

M.insert (e) E must be a value_type type, and if e already exists, it remains m unchanged, returning a pair type that contains a map iterator that points to the element with the key E.first, and an object of type bool.

M.insert (Beg, end) inserts an iterator beg the element in the end range

M.insert (ITER, E)


Five. Find and read the elements in the map


1. Count

M.count (k) returns 1 if key k is found, otherwise 0.

2. Find

M.find (k) if K is found, returns an iterator to the element, otherwise returns an iterator that exceeds the end.


Six. Removing elements from the map

M.erase (k) Delete element with key K

M.erase (p) Delete the element that the iterator P points to

M.erase (b,e) Delete elements in a range


Seven. Traverse

Map < string, int >::const_iterator iter= m.begin ();

while (iter! = M.end ())

{

....

}

C + + Map

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.