C++中的map容器_第1页
C++中的map容器_第2页
C++中的map容器_第3页
C++中的map容器_第4页
C++中的map容器_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+中的map容器内容提耍:在标准模板库(STL)中提供了很多的容器它们是存储对彖的对線。本文主耍介绍 STL 中的关联容器 map 容器,内容包括 map: begin* map:: clear、map: : count、map: : empty、 map:: end等27种两数。本容器是以模板的形式设计的,能应用J:广泛的数据类型。关键字:begin、clear, count、empty> end、find引言:map是一个容器,它用J储存数据并且能从一个数据集介中取出数据。它的数据组成包 倉两项,一个是它的数据值,一个是用排序的关權字。其中关键字是能一的,它用将数据自动 排序。而每

2、个元素的数据值与关键字无关,可以苴接改变。正文:1 map容器map容器简介:map是一个容器,它用J储存数据并J1能从一个数据集合屮取出数州。它的数据组成包含两项, 一个是它的数据值,一个是用J排序的关键字。其中关键字是惟一的,它用将数据门动排序。而 每个元素的数据值与关键字无关,可以直接改变。需加载的头文件:#include<map>using namespace std;模板原型:template <class Key,class Type.class Traits = less<Kev>wJ yclass Allocator-allocator<pa

3、n <const Key, Type> >>说明:表1为map的参数说明。舉1 map的参败说明参数含义Key存储在map容器中的关键字的数据类型Type储存在map容器中的数据值的数拯类熨Traits它是个能提供比较两个尤索的关泄7來决泄它们在map 容器中的相对位用它足可选的,它的默认值是less<key>Allocator它代表存储管理设备。它是可选的,它的默认值为 allocator<pair <const Key. Typemap容器有以下的特点:(1)它是一个相关联的容器,它的人小可以改变,它能根据关键字來提高读取数据能力。(2)它提

4、供一个双向的定位器來读写取数据。(3)它己经根据关键字和一个比较函数来排好序。(4)它的每一个尤素的关键字都是惟一的。(5)它是一个模板,它能捉供一个-般且独立的数据类型。成员变量:map的成员变最说明如表2所示。表2 map的成员变说明成员变虽功能说明Allocatoi type对彖分配器Const.iterator提供一个双向的定位器,它能读取map中的一个常元Const pointei它能提供到一个常元的指针Const.reference一个常元地址Const_reverse_iterator捉供-个双向的崔位器,使得能够庄map容器中读収 任盘一个常值元索Difference_type

5、它提供map容器中山定位器所指定的范1仰内的元素的 个数Iterator提供-个双向入口定位器.使得能够在map中读取或 者修改元素key compare它足捉供-个元索间的关键了的次序关系的函数key.type它描述毎一个元索的关键字mapped type它表示存储在map容器中的数据类型pointer提供一个指向mjp中的某元素的指针refeience捉供在map容器中的一个密元的地址reverse.iterator在反向的map容器中提供一个双向的入口定位器,使 得能够读取或者修改元索size typemap容器中元素个数Value_type它提供一个能根据关铤了來比较两个元索的相对位代

6、 的函数卜面介绍map的成员西数。1J map:: begin功能:返回第个元索的泄位器(iterator)的地址。语法:const_iterator begin ( ) const;iterator begin ();说明:当返回的第一个元素的地址值为一个常值定位器则map不会被修改。 当返回的第一个尤素的地址值为一个定位器(12KH0T),则map可被修改。函数返回值:返回一个指向第一个元索的双向泄位器地址。示例:程序编号:1程序功能说明:用begin來定位到cu的开始位置,并打卬出该元索。include <map> include <iostream> int m

7、ain ()using namespace std;map <int,char> ctr;map <mt,char>:: iteratoi cp;ctr.inseit (pair <int,char> (l/af);ctf.insert (pan <int,chai> (2/b*);cp=ctr.begin (); 定位到ctr的开始位置cout«MThe first element is: M«cp->second«endl; 打印出第一个元索探 2 探return 0;运行结果:运行结果如图1所示。图1程序

8、运行结果1.2 map:: clear功能:将一个map容器的全部元索删除。语法:void clear ();说明:clear会删除map容器的全部尤索。函数返回值:无。示例:y* *41 Hi*程序编号:2程序功能说明:先创建一个map容器,再用clear 数清空,授后打印是否为空的信息。*拿*拿*拿*拿*拿*拿*拿*拿*/include <map>#include <iostream>int mam ()using namespace std;map <int,char> ctr;ctr.inseit (pan <nit,char> (1/a

9、"); ctr.inseit( pair <int,char> (2/bf);(32);清空map容器/map容器为空时ctr.inseit (pair <int,char> ctr.cleai ();if (ctr.empty ()cout«MThe container is emptyH«endl;/map容器为卄空时cout«HThe container is not emptyH«endl; return 0;运行结果:运行结果如图2所示。*C: Dftrw>ent a and Sett.日E 口图2程序运

10、行结果探 3 探1.3 map:: count功能:返回对应某个关键字的元素的个数。语法:size_type count (const Key& _Key)const;说明:_Key是要进行匹配的关键字的值。该函数的返回值的范围是low_bound (_Key) ,uppei_bound (_Key)。因为map容器的关键字是惟一的,故它只能取0或者1。函数返回值:当map容器包含了关键字为_Key的这个元索时,返回1,否则返回0。示例:/*程序编号:3程序功能说明:先创建-个map容器,再用count函数來求出关键字为1的元素的个数。sic*"*/#include <

11、map>井include <iostream>mt main ()using namespace std;map <int,char> ctr;ctr.inseit (pair <int,char> (l/a*);ctr.insert ( pair <int,char> (2/b*);cti.insert (pan <int,chai> (l/cf); int i;i=cti.count (1); 求出关键字为1的尤索的个数 if (10)cout«HThere is no such key?H<<endl

12、; elsecout«nThe number of key is: H«i«endl; return 0;运行结果:运行结果如图3所示。图3程序运行结果1.4 map:: empty功能:测试一个map容器是否为空。语法:bool empty ( ) const;说明:empty函数用J测试一个map容器是否为空。函数返回值:当容器map为空时,返回tine,否则返回false。 示例:程序编号:4程序功能说明:创建一个map容器,打印是否为空的信息。III* */include <map> 并include <iostream>int m

13、ain ()using namespace std;map <int,char> ctr;if (cti.empty ( )/map 容器为空时cout«HThe container is emptvH«endl;else/map容器为非空时cout«HThe container is not emptyH«endl;letuin 0;运行结果:运行结果如图4所示。1.5 map:: end功能:返回最后一个元素后而的定位器(iteKKOl)的地址。语法:const.iterator end ( ) const;iterator end ()

14、;说明:end国数用J:测试一个定位器是否已达到它的尾部。函数返回值:返回-个指向般后一个尤索后面的双向定位器地址。当map容器为空时,结果没定义。 示例:/*“*程序功能说明:先初始化一个map.再打印其中的内容,最后显示出最后一个尤素的值。include <map>include <iostream>using namespace std;mt print (map <int,int> c) 用扌印一个 mapmap <int,int>:: const_iterator cp;for (cp=c.begin ( ) ;cp?=c.end (

15、) ;cp+)让cp从c的开始到结束打印cp对应的值cout«cp->second«H ”;return 0;int main ()map <intant> ctr;map <intunt>:: const.iterator cp;int i;for (i=0;i<3;i+) cti.insert (pair <int,mt> (i,i);给ctr赋值cout«MThe ctr is: ”;print (ctr);调用子程序來打印ctr的内容cout«endl«HTlie last element

16、 is:cp=ctr.end ();让cp指向最后一个尤素的位置cout«cp->second«endl; 显示虽后一,个元索的值 letuin 0;运行结果:运行结果如图5所示。图5程序运行结果1.6 map:: equal_range功能:返回一对定位器,它们分别指向第一个人或等r给定的关键字的元素和第一个比给定的关键 字大的元素。语法:pan <const_iteiatoi, const_iterator> equal_range (const Key& _KevJJ)const;pair <iterator, iterator>

17、 equal_range (const Kcv& KevJJ)const;说明:_Key是一个用于排序的关键字。函数返回值:返回一对定位器。要从第一个定位器屮取得数据,可用pr.firsto从第二个定位器中取得数据,则用pr.second.示例:/*程序编号:6程序功能说明:先初始化一个map.再打印其中的内容,最后打印出关键字>=2或>2的元素。*/幷include <map>include <iostream>using namespace std;int print_one_item (map <int,int>:: const_i

18、terator cp)/用 J 打印 map 的一个元索cout«f, (M«cp->first«M , H«cp->second«M)”;return 0;int print (mapc) 用 J打印_个 mapmap <intant>:: const_iterator cp;for (cp=c.begin ( ) ;cp?=c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值pnnt_one_item (cp);调用子程序来打印一个尤素return 0;int main ()map <int4i

19、it> ctr;pair <map <>:: const_iterator, map <int,int>:: const_iterator> p;int i;for (i=0;i<=3;i+) ctr.insert (pair <int,int> (i,i);给ctr赋值cout«f,The ctr is: ”;print (ctr);调用子程序來打印ctr的内容cout«endl;p=ctr.equal_iange (2);if (p.firstf-ctr.end ()cout«HThe

20、first element which key >= 2 is: M;pimconeitem (p.first); 调用子程序來打印一项cout«endl;if (p.second?=ctr.end ()cout«MThe first element which kev > 2 is:Jprmt.one.item (p.second ); cout«endl;return 0;运行结果:运行结果如图6所示。八 Dnntraonts and Settinc®t anckaoiiifln. 日回田图6程序运行结果1.7 map:: erase功能

21、:将一个或一定范闲的尤素删除。 语法:iterator erase (iterator _Wlieie);iterator erase (iteiator _First, iterator _Last);size_type erase (const key_type& _Key);说明:表3为erase的参数说明。* 3 erase的發数说明参数含义.Where表示耍删除的元索的位置.First第一个被删除的元素的位誉.Last第一个不被删除的元索的位置-Key从map容器中删除的元索的关健了的值因为没仃重新分配空间,故只有対应丁被删除元索的iterator和lefeience失效。

22、注意:它不会抛出任何的exception。函数返回值:前两个函数返回个指向第个没被删除的元索的双向定位器,如果不存在这样的元素,则返 回map容器的末尾。第三个幣数返冋被删除的元素的个数。示例:程序编号:7程序功能说明:用erase 11数将ctr的第二个尤素删除.*寒*拿*拿*/#include <map>include <iostream>using namespace std;#define len 5int print_one_item (map <intjnt>:: const_iterator cp)用打印 map 的一个元素cout«

23、M (M«cp->first«H , °'v<cp>secondvv”)H;return 0;int print (map <int,int> c)用 J打印一个 mapmap <intant>:: const.iteiatoi cp;for (cp=c.begin ( ) ;cp!=c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值pnnt_one_item (cp);调用子程序來打印一个元素return 0;int main ()map <int,int> ctr;map <

24、mt,int>:: iteiatoi cp;int i;for (i=0;i<len;i-H-) ctr.insert (pair <int,int> (i,i) ;/下面先给 ctr 赋值cout«Hctr is: H;print (ctr);调用子程序,把ctr打印出来cout«endl;cout«MAfter erase the second element cti is: ”;cpctr.begin ();cp+;ctr.erase (cp);擦除ctr的cp位置的尤素print (ctr);调用子程序,扌巴cm打印出來cout&#

25、171;endl;return 0;运行结果:运行结果如图7所示.图7程序运彳亍结果1.8 map:: find功能:求出与给定的关键字相等的元素的定位器。语法:iterator find (const Key& _Key);const.iterator find (const Key& _Key)const;说明:_Key是耍进行搜索的关键字的值。该函数会返回一个指向关键字为_Key的元索的定位器。当返回的第一个元素的地址值为一个常值定位器(_itenH(M),则map可通过它不会被修改。 当返回的第一个尤素的地址值为一个定位器(henuor),则map可通过它被修改。函数返

26、回值:找到该尤索时,返回-个指向关键字为_Key的元索的定位器,否则返回一个指向map容器的 结束的定位器。示例:程序编号:8程序功能说明:用将key=2的兀索打印出来。* “*“*“*m*“*m*/include <map>include <iostream>using namespace std;define len 5mt print_one_item (map <intant>:: const_iterator cp)用 J扌印 map 的一个元索cout«H (M«cp->first«M , H«cp-

27、>second«H)”;return 0;int print (map <int,int> c) 用 J打卬一个 map 容器map <intant>:: const_iterator cp;for (cp-c.begin ( ) ;cp!c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值pnnt.onejtem (cp) ; /调用子程序来打印一个元素return 0;mt mam ()map <intant> ctr;map <int,int>:: iterator cp;int 1;for (i=0;i&l

28、t;len;i+) cti.insert (pair <int,int> (ij);下面先给CM赋值 cout«Mctr is: H; print (ctr);/调用子程序,把Ct【打印出来cout«endl; cp=ctr.find (2); if (cp!=ctr.end ()査找kev=2的元素 找到时,打印出來cout«HThe element whose kev = 2 is:J pnnt_one_item (cp);cout«endl;else找不到时cout«HThere is no element whose key

29、 = 2H«endl; retuin 0;运行结果:运行结果如图8所示.: Xl'mraa Fi 1 r i ii rrnnft Vi -run 1 St «idi n . HE 口图8程序运行结果1.9 map:: geCallocator功能:返回一个构造该map容器的allocator的一个副本。语法:Allocator get_allocator ( ) const;说明:容器map的allocator指明一个类的“储管理。默认的allocator能捉供STL容器高效的运行。 函数返回值:该容器的allocatoro示例:/拿*审拿*車車拿*車冷*車*4(車

30、*車*車*4r4i車*程序编号:9程序功能说明:用ctr的allocator来创建CM2。幷include <map>include <iostream>using namespace std;int main ()map <int,char> ctr;cti.inseit (pan <int,char> (l/a*);map <int,char> cti2 (less<int> ( ) ,cti.get_allocatoi (); cout«Mctr2*s size is: H«ctr2.size&#

31、171;endl;return 0;运行结果:运行结果如图9所示。C' *C!DO(:IIll!BTS AND SKI |凶ctr2J s 3 izc is: 1Pt*ess any key tn continuezJrni2J图9程序运疔结果1.10 map;: insert功能:将一个尤素或者一定数昴的元素插入到map的特定位置中去。 语法:pair <iterator, bool> insert ( const value_type& _Val);iterator insert (iterator _Where, const value_type& _

32、Val);template<class Inputlterator>void insert (Inputlteratoi _First,Inputlteiatoi _Last);说明:表4为参数的说明。表4 insert的參IS说阴参数含义Wheie第一个被插入到map的元索的位过Val插入的参数的值First第一个被插入的位腔.Last第一个不被捕入的位左如果能在.Where后而迅速地插入,那么只耍很短的时间。第三个成员函数将范用为_First,_Last 中的尤素插入。探 15 探函数返回值:第一个旳数返冋一对值,当插入成功时,bool=ti-ue,当耍插入的元索的关键字与已有

33、的参数的 值相同,则bool=falsc, Ifuiteiatoi指向插入的位置或者已存在的元索的位置。第二个函数返回指向插入位置的定位器。示例:/拿*拿*4(拿*拿*拿*拿*程序编号:10程序功能说明:利用insert 数给ctr赋值。>|i*>|(*4i*4i*4n|i*4i*4i*4i* *«*/幷include <map>include <iostream>using namespace std;#define len 5int print_one_item (map <intjnt>:: const_iterator cp)用

34、于打E卩map的一个元素cout«H (H«cp->first«M , H«cp->second«M)”;return 0;int print (map <int,int> c)用于打印一个mapmap <intant>:: const_iteratoi cp;for (cp=c.begin ( ) ;cp?=c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值pnnt_one_item (cp);调用子程序打印一个元素renirn 0;int main ()map <intant>

35、; ctr;map <int,int>:: iterator cp;uit i;for (i=0;i<len;i+) ctr.insert (pair <intjnt> (i j);下面先给CM赋值couctr is: H;print (ctr);调用子程序,把CU打印出來cout«endl;return 0;运行结果:运行结果如图10所示。图10程序运行结果卩1.11 map:: key_comp功能:取得一个比较对彖的副木以对map容器中的兀素进行排序。语法:key_compare key_comp ( ) const;说明:存储对彖定义了一个成员凶

36、数:bool operator (const Key& _Lef= const Key& _Right);_Left与_Right在次序比较中不同时,返回true,否则返回false。 函数返回值:取得一个刈map容器中的元素进行排序的比较对象示例:程序编11程序功能说明:先取得一个key_compare对參,再用此对彖比较1和2。* *“*/include <map> include <iostream> using namespace std; int main ()map <int jnt> ctr;map <int, int,

37、less<int> >:: pare kc = p () if (kc ( 1,2 )cout«Hkc (1,2) is tmetf«endl;elsecout«Hkc (1,2) is falseM«endl;retuin 0;运行结果:运行结果如图11所示。-C:DOCTHHMTS AWD SETTTIG耳回口kc<1.2> ic truePress any key to continuezlEL丄图11程庁运彳j结果1.12 map:: lower_bound功能:求出指向第一个关键字的值是人J等J一个给定值的元素的定

38、位器。 探 14 探语法:iterator lower.bound (const Key& _Key);const.iterator lower_bound (const Key& _Kev)const;说明:_Key是一个用于排序的关键字。当返回值为一个const .iterator,则map容器不会被修改。当返回值为一个iterator,则map容器可被修改。函数返回值:返冋一个指向第一个关犍字的值是大于等一个给定值的尤素的定位器,或者返冋指向map容 器的结束的定位器。示例:程序编号:12程序功能说明:先初始化一个map,再打印其中的内容,垠后将关键字比2人的尤索打印出來

39、。*車*拿*車*車*拿*拿拿*車拿車拿*車*車*車*車車*車*/include <map>include <iostream>using namespace std;int print_one_item (map <intant>: : const_iterator cp)/用 J:扌J 卬 map 的 个元素cout«M ( M«cp->first«H , H«cp->second«M) M;return 0;int print (map <int,int> c) 用 J打印一个 m

40、apmap <int4nt>:: const_iteiatoi cp;for (cp-c.begin ( ) ;cp?-c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值print_one_item (cp);调用子程序来打印一个尤素return 0;int main ()map <intjnt> ctr;map <int,int>: : const_iterator cp;int i;for (i=0;i<=3;i+) ctr.insert (pair <int,int> (i,i);给ctr赋值cout«f,

41、The ctr is: ”;print (ctr);调用子程序来打印的内容cout«endl;cp-ctr.lowei_bound (2);if (cp?-ctr.end ()cout«HThe first element which key >= 2 is: ”; pimt_one_item (cp);调用子程序來打印一项cout«endl;leturn 0;运行结果:运行结果如图12所示。C:DOCUIE1TS ATO SETT»CSTAICHAOQI. | ho ctr io: (0 0> <1 1> <2 , 2&g

42、t; <3 3> I ho f iret clcreont which hoy >v 2 is: C2 » 2> Press «ny key to continue.叵i上图12程序运行结果1.13 map:: map功能:map的构造函数。语法:map ();explicit map (const Traits& _Comp);explicit map (const Traits& _Comp、const Allocator& _A1);map (const _map& _Right);templa(e<cla

43、ss Inputlteiator>map (Inputlterator .First,Inputiterator .Last);template<class Inputlterator>map (Inputlterator .First,Inputlteiatoi _Last.const Traits& _Comp);template<class Inputlteiator>map (Inputiterator .First,探 21 探Inputlteiator .Last, const Traits& _Comp. const Allocato

44、r& _A1);说明:表5为map两数的参数说明。>5 map函败的參效说明含义A1一个分配黯类Comp个用丁比较的函数它的默认值为hash compaie.Right一个map的拷WFirst彼拷贝的第一个元索的位丘.Last被拷贝的最后一个元索的位宽所有的构造换数都存储一个分配器和初始化map容器。所有的构造换数都存储一个Tnuts类型的换数对彖,它用对map容器的尤索进行排序。 头三个构造函数创建一个空的初始map容器。第四个构造函数创建一个.Right容器的副本。后三个构造换数拷贝在范I虬First_Last内的一个mjp容器。函数返回值:无。示例:严* ”*”*n* “

45、*程序编号:13程序功能说明:先定义一个ct"的map容器,再构造一个以cul相同的容器。sic*/幷include <map>include <iostream>using namespace std;int pnnt_one_item ( map <intunt>; : const_iterator cp)Jfl J扌J E卩 map 的一个丿i 素cout«f, (M«cp->fiist«M , H«cp->second«M)”;return 0;int print (map &l

46、t;int,int> c)用丁打卬一个 mapmap <intant>:: const.iterator cp;for (cp=c.begin ( ) ;cp!=c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值pnnt_one_item (cp);调用子程序来打印一个尤素return 0;int main ()map <intant> Ctrl; 创建一个有两个为字母为®的map mt i;for (i=0;i<=3;i+) Ctrl.insert (pair <intant> (14);给Ctrl赋值map <

47、;int,int> ctr2 (Ctrl);创建一个以Ctrl相同的map容器cout«f,The ctr2 is: ”;print (ctr2);打印 ctr2 的内容cout«endl;leturn 0;运行结果:运行结果如图13所示。C:D0CUIEITS £ID SETTIVGSTlICHiOQIiVG. QQIIhe ctr2 is: <0 . 0> <1 . 1> <2 . 2> <3 3>APreoo Any Koy to cont flnu«创M1图13程序运彳亍结果1.14 map:

48、: max.slze功能:计算map容器的最大长度。语法:size_type max_size ( ) const;说明:max_size会返回map容器的最长度。函数返回值:返回map容器可能的址人长度。示例:,*“*程序编号:14程序功能说明:求出一个map容器的可能最大的长度。样include <map> #include <iostream>using namespace std;define len 5int main ()map <int,int> ctr;map <int,int>:: size_tvpe st;int i;for

49、(i=0;i<len;i+) cti.Hisert (pair <int jnt> (i,i);先给cm赋值匹cout«Mthe max_size of ctr is: st=ctr.max.size (); cout«st«endl;retuin 0;运行结果:运行结果如图14所示。 C:DOCU1E1TS Afli) SETTIRGST 回pKthr m«x_si?e of ctr is:1A73741823Presskey to continueHIT上图14程序运行结果1.15 map:: operator功能:将一个给定的值插

50、入到map容器中去。语法:Type& openHoi(const Kev& _Kev);说明:.Key是被插入的元素的关键字的值。注意:当map容器中找不到_Key的值时,则插入到map容器中,且值是一个默认值。 函数返回值:返回一个Key位宣的冗素的地址("fewnce)。示例:/*拿*拿拿*拿*拿*拿拿*車*車*程序编号:15程序功能说明:用operator在map容器中插入两个元素幷include <map>/include <iostream>using namespace std;用打印map的一个元素int pnnt_one_ite

51、m ( map <intjnt>:: const.iterator cp)cout«f, (M«cp->fust«H , H«cp->sccond«t,)”;return 0;int print (map <int,int> c) 用 J扌J 印一个 mapmap <intant>:: const.iterator cp;for (cp=c.begin ( ) ;cp!=c.end ( ) ;cp+)让cp从c的开始到结束打印cp对应的值piint_one_item (cp);调用子程序來打印一个

52、元素return 0;int mam ()map <intjnt> ctr;int 1;for (i=0;i<=3;i+) ctf.mscrt (pan <mtant> (i,i);/给ctr赋值cout«MThe ctr is:调用子程序来打印CM的内容调用子程序来打印CM的内容print (ctr); cout«endl; ctrl=10; ctr7;cout«,The ctr now is: punt (ctr);cout«endl;return 0; 运行结果:运行结果如图15所示。#include <map&

53、gt;探 25 探#include <map>探 # 探图15程序运彳亍结果#include <map>探 # 探L16 map:: operator!=功能:测试该map容器的左边与右边是否相同。语法:bool operator!-(const map <Key. Type, Traits, Allocator>& _Left,const map <Key. Type, Traits, Allocator>& _Right);说明:_Left和_Right是待比较的两个map容器.两个map容器相等,当仅当它们的尤索个数相等且同

54、一个位置上的值相等。 函数返回值:当_Left和_Right不同时,返回True,否则返回False« 示例:/*程序编号:16程序功能说明:比较两个map容器是否相等。*” *“* /include <iostream> int main ()using namespace std;map <int,char> ctrl,ctr2;int i;for (i=0;i<3;i+)Ctrl.insert (pair <int,char> (i/ai); ctr2.insert (pair <int,char> ( i/Ai);if (

55、ctil!=cti2)当 Ctrl 与 ct2 不同时cout«"They are not equal,«endl;else当Ctrl与ctr2相同时cout«*'They are equalH«endl;return 0;运行结果:运行结果如图16所示。-C:DOCU1EJITS AID SETTIIC. | |XThey ae not equlPress any key to continuoJN1>图16程用运行结果1.17 map:: operator<功能: 测试该map容器的左边是否小右边。语法:bool operator-(const map <Key, Type, Traits, Allocator&g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论