C + + STL map usage

Source: Internet
Author: User

Map is a standard C + + container, it provides a good one-to-one relationship, in some programs to build a map can play a multiplier effect, summed up some map basic simple and practical operation!
1. Map constructor;
map<string, int >mapstring; Map<int, String >mapint;
Map<sring, char>mapstring; map< Char,string>mapchar;
Map<char,int>mapchar; Map<int, Char >mapint;

2. Map add data;
Map<int,string> maplive;
1.maplive.insert (pair<int,string> (102, "aclive"));
2.maplive.insert (Map<int,string>::value_type (321, "Hai"));
3, maplive[112]= "April";//map the simplest and most common inserts to add!

Find the elements in the 3,map:

The Find () function returns an iterator that points to an element with a key value, and returns an iterator to the tail of the map if it is not found.

Map<int, string >::iterator l_it;;
L_it=maplive.find (112);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else cout<< "Wo find" <<endl;


Deletion of elements in 4,map:
If you delete 112;
Map<int, string >::iterator l_it;;
L_it=maplive.find (112);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else Maplive.erase (L_IT); Delete 112;

Sort problem with 5.map:
The elements in map are automatically sorted by key in ascending order, so you cannot use the Sort function for map

Basic operation function of 6.map

C + + maps is an associative container that contains "keyword/value" pairs

Begin () returns an iterator pointing to the map header

Clear () Delete all elements

COUNT () returns the number of occurrences of the specified element

Empty () returns True if Map is empty

End () returns an iterator pointing to the end of the map

Equal_range () returns an iterator to a special entry

Erase () Delete an element

Find () finds an element

Get_allocator () returns the map's Configurator

Insert () Inserts an element

Key_comp () returns the function that compares the element key

Lower_bound () returns the key value >= the first position of a given element

Max_size () returns the maximum number of elements that can be accommodated

Rbegin () returns a reverse iterator pointing to the tail of the map

Rend () returns a reverse iterator pointing to the map header

Size () returns the number of elements in the map

Swap () Swap two maps

Upper_bound () returns the key value > the first position of a given element

Value_comp () returns a function that compares the element value

Using the Map Demo

1 voidcloaddlldemodlg::onbnclickedbutton17 ()2{//STL MAP3     /*define a map with CString as value Key,int*/4Std::map<cstring,int>Mapdemo;5 6      for(inti =0; I <Ten; i++)7{//Add Data8 CString strkey;9Strkey.format (_t ("key:%d"), i);Ten         /*Method 1*/ OneMapdemo.insert (Std::make_pair<cstring,int>(strkey, i)); A         /*Method 2*/ -         //Mapdemo.insert (map<cstring, Int>::value_type (strkey, i)); -     } the  -     /*Find Data*/ - CString Strfindkey; -Std::map<cstring,int>:: iterator Itfind; +      for(intp =0; P < the; p++) -     { +Strfindkey.format (_t ("key:%d"), p); AItfind =Mapdemo.find (strfindkey); at         if(Itfind! =mapdemo.end ()) -{//Find Data -             intNval = itfind->second; -             if(9==nval) -{//Delete this piece of data - mapdemo.erase (itfind); in             } -         } to     } +}

C + + STL map usage

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.