C ++ Map usage

Source: Internet
Author: User
Tags map class

1. 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.

2. map Functions

Automatically create the corresponding Key-value. Key and value can be any type you need.
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.
Insert Key-Value records quickly.
Quickly delete records
Modify the value record based on the Key.
Traverse all records.
3. 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;

4. Insert elements into 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 and the value is an empty string, after the insertion is complete, assign the string to "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 "))

5. 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.

6. Delete elements from map

Remove an entry from a map using erase ()

The member method is defined as follows:

Iterator erase (iterator it); // delete an entry object
Iterator erase (iterator first, iterator last); // delete a range
Size_type erase (const Key & key); // Delete by using the keyword
Clear () is equivalent to enumMap. erase (enumMap. begin (), enumMap. end ());

Use of C ++ STL map

The following example shows how to insert, search, traverse, and delete STL map in C ++:

# Include <map>
# Include <string>
# Include <iostream>
Using namespace std;

Void map_insert (map <string, string> * mapStudent, string index, string x)
{
MapStudent-> insert (map <string, string >:: value_type (index, x ));
}

Int main (int argc, char ** argv)
{
Char tmp [32] = "";
Map <string, string> mapS;

// Insert element
Map_insert (& mapS, "192.168.0.128", "xiong ");
Map_insert (& mapS, "192.168.200.3", "feng ");
Map_insert (& mapS, "192.168.200.33", "xiongfeng ");

Map <string, string >:: iterator iter;

Cout <"We Have Third Element:" <endl;
Cout <"-----------------------------" <endl;

// Find element
Iter = mapS. find ("192.168.0.33 ");
If (iter! = MapS. end ()){
Cout <"find the elememts" <endl;
Cout <"It is:" <iter-> second <endl;
} Else {
Cout <"not find the element" <endl;
}

// See element
For (iter = mapS. begin (); iter! = MapS. end (); iter ){

Cout <"|" <iter-> first <"|" <iter->
Second <"|" <endl;

}
Cout <"-----------------------------" <endl;

Map_insert (& mapS, "192.168.30.23", "xf ");

Cout <"After We Insert One Element:" <endl;
Cout <"-----------------------------" <endl;
For (iter = mapS. begin (); iter! = MapS. end (); iter ){

Cout <"|" <iter-> first <"|" <iter->
Second <"|" <endl;
}

Cout <"-----------------------------" <endl;

// Delete element
Iter = mapS. find ("192.168.200.33 ");
If (iter! = MapS. end ()){
Cout <"find the element:" <iter-> first <endl;
Cout <"delete element:" <iter-> first <endl;
Cout <"==================================" <endl;
MapS. erase (iter );
} Else {
Cout <"not find the element" <endl;
}
For (iter = mapS. begin (); iter! = MapS. end (); iter ){

Cout <"|" <iter-> first <"|" <iter->
Second <"|" <endl;

}
Cout <"==================================" <endl;

Return 0;
}

 

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.