C + + Learning notes (eight)-map

Source: Internet
Author: User

Map<key, Value> is ordered by key, key cannot be duplicated.

1. Map.lower_bound (): Press key to find, if the found key exists, return to that location, if there is no value returned greater than the minimum key location

1#include <iostream>2#include <map>3 using namespacestd;4 5 intMain ()6 {7std::map<Char,int>Mymap;8std::map<Char,int>:: iterator Itlow,itup;9 Tenmymap['a']= -; Onemymap['b']= +; Amymap['b']= -;//The key value is not duplicated, and the value of B is updated to -mymap['C']= the;//values of value can be repeated -mymap['D']= the; themymap['e']= -; -  -    for(std::map<Char,int>::iterator It=mymap.begin (); It!=mymap.end (); ++it) -Std::cout << It->first <<"="<< It->second <<'\ n'; +cout<<Endl; -                                           //This procedure actuallycannot be inserted successfully because key=b already exists and value is not updated +Itlow=mymap.lower_bound ('b');//if the key value B exists, Itlow points to B, otherwise it points to the location of the smallest key larger than B AMymap.insert (itlow,std::map<Char,int>::value_type ('b', +)); //Insert map< ' B ',40> to the position pointed to by Itlow  at  -    for(std::map<Char,int>::iterator It=mymap.begin (); It!=mymap.end (); ++it) -Std::cout << It->first <<"="<< It->second <<'\ n'; -cout<<Endl; -  -   CharC='a'; in    while(1) -{//Enter a character and insert toCin>>C;  +Itlow =Mymap.lower_bound (c); -     if(Itlow! =mymap.end ()) thecout<<"Insert"<<c<<"To Position:"<<itlow->first<<" , "<<itlow->second<<'\ n'; *Mymap.insert (Itlow, map<Char,int>::value_type (c, the+c)); $      for(std::map<Char,int>::iterator It=mymap.begin (); It!=mymap.end (); ++it)Panax NotoginsengStd::cout << It->first <<"="<< It->second <<'\ n'; -cout<<Endl; the   } the   return 0; + }

Output Result:

1 A =2 B = +3 C =4 d =5 e =6 7 A =8 B = +9 C =Ten d = One e = A  - d //did not insert successfully, D is the value  of the original - Insert D to Position:d, the A = - B = + - C = - d = + e = -  + z //insertion position in Map.end  A A = at B = + - C = - d = - e = - z = 282 -  in x - Insert X to position:z, 282 to A = + B = + - C = the d = * e = $ x = 280Panax Notoginsengz = 282

2. Map.key_comp (), returns true only if the first parameter is less than the second one.

The function return value is bool type, the input is two key value, Key_comp () (A, B), 1 is returned when a<b, otherwise 0 is returned.

1#include <iostream>2#include <map>3 using namespacestd;4 intMain ()5 {6std::map<Char,int>Mymap;7 8mymap['a'] = -;9mymap['C'] = -;Tenmymap['x']=101; Onemymap['y']=203; Amymap['Z']=103; -  -Std::cout <<"Mymap contains:\n"; themap<Char,int>::iterator it =Mymap.begin (); -    for(it = Mymap.begin (); It!=mymap.end (); it++) -cout<<it->first<<"="<<it->second<<'\ n'; -cout<<Endl; +  -map<Char,int>::iterator pit =Mymap.begin (); +pit++; Apit++;//Pit points to the third element map<x,101> at  -it =Mymap.begin (); -    for(it = Mymap.begin (); It!=mymap.end (); it++) -   { -cout<<"Key Comp Result:"<<mymap.key_comp (It->first, Pit->first) <<Endl; -   } incout<<Endl; -   return 0; to}

Results:

1 Mymap contains:2A = -3c = -4x =1015y =2036z =1037 8Key Comp Result:19Key Comp Result:1TenKey Comp Result:0 //At this time It=pit  OneKey Comp Result:0 AKey Comp Result:0

C + + Learning notes (eight)-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.