C + + Map

Source: Internet
Author: User

What is 1.MAP?

Today we're going to learn map easy, Map is an association is a container, adding and deleting elements in a map is very easy. You can modify a specific node without affecting other nodes, since map is an associative container, we have to make it clear that in map we can modify the value of the corresponding value according to the value of the key, but not the value of the key, Key--value. The biggest feature is that the value of key and value can be any type (

Internal type you or a user-defined type).

2. Map Features:

(1) Rapid establishment of Key--value index. Key and value can be of any type.

(2) The query time complexity of map is Logn, that is, if it contains 1000 records, the maximum number of queries is 10 times, 1,000,000 records, up to 20 times.

(3) Quick Insert Key-value record.

(4) quickly delete records

(5) Modify the value record according to key.

(6) Traverse all records.

3. Use map

Use map to include the header file where the map class resides

#include <map>//Note that the STL header file has no extension. h

The map object is a template class that requires two template parameters for the keyword and storage object:

Std:map<int, string> personnel;

This defines an int as an index and a pointer to a string that is associated with it.

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. Inserting elements into a map

Changing the entries in the map is simple because the map class has overloaded the [] operator

ENUMMAP[1] = "one";

ENUMMAP[2] = "both";

.....

This is very intuitive, but there is a performance issue. Insert 2 o'clock, first find the key 2 in Enummap, not found, and then insert a new object Enummap, the key is 2, the value is an empty string, after the insertion is completed, the string is assigned "two"; This method assigns each value to the default value, which is then assigned to the displayed value, which is more expensive if the element is a class object. We can avoid the overhead in the following ways:

Enummap.insert (Map<int, cstring>:: Value_type (2, "a"))

5. Find and get the elements in the map

The subscript operator gives the simplest way to get a value:

CString tmp = enummap[2];

However, it is only true if there is an instance of the key in the map, or an instance is automatically inserted, and the value is the initialization value.

We can use the find () and count () methods to discover whether a key exists.

Find out if the map contains a keyword entry with the Find () method, the parameter passed in is the key to look up, where you need to refer to the Begin () and end () two members, representing the first and last entry in the Map object, the type of the two data is iterator.

int nfindkey = 2; Key to look for

Define an entry variable (actually a pointer)

Udt_map_int_cstring::iterator it= Enummap.find (Nfindkey);

if (it = = Enummap.end ()) {

Didn't find

}

else {

Found it

}

The iterator data type obtained by the method of the Map object is an std::p air object, including two data Iterator->first and iterator->second representing the keywords and stored data respectively

6. Delete elements from the map

Remove an entry from a map with erase ()

The member method is defined as follows

    1. Iterator Erase (iterator it); Delete by an Entry object
    2. Iterator Erase (iterator first, iterator last); Delete a range
    3. Size_type Erase (const key& Key); Delete by keyword

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

Use of C + + STL map

The following are examples of inserting, locating, traversing, and deleting STL maps in C + +:

1#include <map>2#include <string>3#include <iostream>4 using namespacestd;5 6 voidMap_insert (Map <string,string> *mapstudent,stringIndexstringx)7 { 8Mapstudent->insert (Map <string,string>:: Value_type (Index, x)); 9 } Ten  One intMainintargcChar**argv) A {  - Chartmp[ +] ="";  -Map <string,string>MapS; the  - //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; A  atcout <<"We have third Element:"<<Endl; -cout <<"-----------------------------"<<Endl; -  - //Find Element -iter = Maps.find ("192.168.0.33");  - if(ITER! =Maps.end ()) {  incout <<"Find the Elememt"<<Endl; -cout <<"It is:"<< Iter->second <<Endl; to}Else {  +cout <<"Not find the element"<<Endl; - }  the  * //See element $  for(iter = Maps.begin (); ITER! =maps.end (); iter) { Panax Notoginseng  -cout <<"| "<< Iter->first <<" | "<< iter-> theSecond <<" |"<<Endl; +  A }  thecout <<"-----------------------------"<<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) {  the  -cout <<"| "<< Iter->first <<" | "<< iter->WuyiSecond <<" |"<<Endl; the  - }  Wu  -cout <<"-----------------------------"<<Endl; About  $ //Delete Element -iter = Maps.find ("192.168.200.33");  - if(ITER! =Maps.end ()) {  -cout <<"Find the element:"<< Iter->first <<Endl; Acout <<"Delete element:"<< Iter->first <<Endl; +cout <<"================================="<<Endl; the Maps.erase (ITER); -}Else {  $cout <<"Not find the element"<<Endl; the }  the  for(iter = Maps.begin (); ITER! =maps.end (); iter) {  the  thecout <<"| "<< Iter->first <<" | "<< iter-> -Second <<" |"<<Endl; in  the }  thecout <<"================================="<<Endl; About  the return 0;  the}

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.