STL map Common Operations)

Source: Internet
Author: User
Tags map class

1. Directory

  1. Introduction to map
  2. Map Functions
  3. Use map
  4. Insert element in map
  5. Search for and obtain elements in map
  6. Delete element from map

2. Introduction to map

Map is an associated container. It features that adding or deleting nodes has little impact on the iterator, except that the Operation node has no impact on other nodes. For the iterator,

You can modify the real value instead of the key.

3. Map Functions

  1. Automatically create the corresponding Key-value. Key and value can be any type you need.
  2. You can quickly search for records based on the key value. The search complexity is basically Log (N). If there are 1000 records, you can search for up to 10 records, 1,000,000 records, and up to 20 records.
  3. Insert Key-Value records quickly.
  4. Quickly delete records
  5. Modify the value record based on the Key.
  6. Traverse all records.

4. Use map

Use map to obtain the header file containing the map class
# Include <map> // note that the STL header file does not have the extension. h.

The map object is a template class and requires two template parameters: keyword and storage object:
Std: map <int, string> personnel;
In this way, we define an index with an int and have the associated pointer to the string.

For ease of use, you can define the type of the template class,

Typedef map <int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;

5. Insert element in map

It is very easy to change the entries in the map, because the map class has already overloaded the [] operator.

EnumMap [1] = "One ";
EnumMap [2] = "Two ";
.....

This is very intuitive, but there is a performance problem. When inserting 2, first find the item with the primary key of 2 in enumMap, and then insert a new object into enumMap. The key is 2,

The value is an empty string. After being inserted, the string is assigned as "Two". This method assigns each value to the default value and then to the displayed value, if the element is a class object,

The overhead is relatively large. We can avoid overhead using the following methods:

EnumMap. insert (map <int, CString >:: value_type (2, "Two "))

6. Search for and obtain elements in map

The subscript operator provides the simplest way to obtain a value:

CString tmp = enumMap [2];

However, this parameter is only applicable to instances with this key in map. Otherwise, an instance is automatically inserted and its value is the initialization value.

We can use the Find () and Count () methods to identify whether a key exists.

The find () method is used to check whether a map contains a keyword entry. The input parameter is the key to be searched. here we need to mention two members: begin () and end,

The first and last entries in the map object are represented respectively. The two data types are iterator.

Int nFindKey = 2; // Key to be searched
// Define an entry variable (actually a pointer)
UDT_MAP_INT_CSTRING: iterator it = enumMap. find (nFindKey );
If (it = enumMap. end ()){
// Not found
}
Else {
// Find
}

The iterator data type obtained through the map object method is a std: pair object, which contains two data iterator-> first and iterator-> second representing the keywords and stored data respectively.

7. Delete element from map

Remove an entry from a map using erase ()

The member method is defined as follows:

  1. Iterator erase (iterator it); // delete an entry object
  2. Iterator erase (iterator first, iterator last); // delete a range
  3. Size_type erase (const Key & key); // Delete by using the keyword

Clear () is equivalent to enumMap. erase (enumMap. begin (), enumMap. end ());

1. Directory

  1. Introduction to map
  2. Map Functions
  3. Use map
  4. Insert element in map
  5. Search for and obtain elements in map
  6. Delete element from map

2. Introduction to map

Map is an associated container. It features that adding or deleting nodes has little impact on the iterator, except that the Operation node has no impact on other nodes. For the iterator,

You can modify the real value instead of the key.

3. Map Functions

  1. Automatically create the corresponding Key-value. Key and value can be any type you need.
  2. You can quickly search for records based on the key value. The search complexity is basically Log (N). If there are 1000 records, you can search for up to 10 records, 1,000,000 records, and up to 20 records.
  3. Insert Key-Value records quickly.
  4. Quickly delete records
  5. Modify the value record based on the Key.
  6. Traverse all records.

4. Use map

Use map to obtain the header file containing the map class
# Include <map> // note that the STL header file does not have the extension. h.

The map object is a template class and requires two template parameters: keyword and storage object:
Std: map <int, string> personnel;
In this way, we define an index with an int and have the associated pointer to the string.

For ease of use, you can define the type of the template class,

Typedef map <int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;

5. Insert element in map

It is very easy to change the entries in the map, because the map class has already overloaded the [] operator.

EnumMap [1] = "One ";
EnumMap [2] = "Two ";
.....

This is very intuitive, but there is a performance problem. When inserting 2, first find the item with the primary key of 2 in enumMap, and then insert a new object into enumMap. The key is 2,

The value is an empty string. After being inserted, the string is assigned as "Two". This method assigns each value to the default value and then to the displayed value, if the element is a class object,

The overhead is relatively large. We can avoid overhead using the following methods:

EnumMap. insert (map <int, CString >:: value_type (2, "Two "))

6. Search for and obtain elements in map

The subscript operator provides the simplest way to obtain a value:

CString tmp = enumMap [2];

However, this parameter is only applicable to instances with this key in map. Otherwise, an instance is automatically inserted and its value is the initialization value.

We can use the Find () and Count () methods to identify whether a key exists.

The find () method is used to check whether a map contains a keyword entry. The input parameter is the key to be searched. here we need to mention two members: begin () and end,

The first and last entries in the map object are represented respectively. The two data types are iterator.

Int nFindKey = 2; // Key to be searched
// Define an entry variable (actually a pointer)
UDT_MAP_INT_CSTRING: iterator it = enumMap. find (nFindKey );
If (it = enumMap. end ()){
// Not found
}
Else {
// Find
}

The iterator data type obtained through the map object method is a std: pair object, which contains two data iterator-> first and iterator-> second representing the keywords and stored data respectively.

7. Delete element from map

Remove an entry from a map using erase ()

The member method is defined as follows:

  1. Iterator erase (iterator it); // delete an entry object
  2. Iterator erase (iterator first, iterator last); // delete a range
  3. Size_type erase (const Key & key); // Delete by using the keyword

Clear () is equivalent to enumMap. erase (enumMap. begin (), enumMap. end ());

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.