CSTL深入理解关联容器multimap和map及其查找操作
关联容器map和multimap为了实现接近于logn的查找(二分查找)效率,将一维的数据组织成二维的树型逻辑结构(红黑树做为底层逻辑结构)。由此,为了维护其元素有序结构,其增加元素的成员函数是insert(),而不是序列容器的pushback()或list、deque容器的pushfront()。
关联容器multimap是一个key与多个value的映射关系。
关联容器map是一个key与一个value的映射关系。
因为map一一映射的特性,所以有额外的成员函数at()和重载操作符operator〔〕。
classification
functionname
map
multimap
constructor
map::map
multimap::multimap
destructor
map::map
multimap::multimap
memberfunctions
at
NA
begin
cbegin
cend
clear
count
crbegin
crend
emplace
emplacehint
empty
end
equalrange
erase
find
getallocator
insert
keycomp
lowerbound
maxsize
operator
operator〔〕
NA
rbegin
rend
size
swap
upperbound
valuecomp
nonmemberoverloads
relationaloperators
swap
1multimap的单值查找查找iteratorfind(constkeytypek);constiteratorfind(constkeytypek)const;
demo:includeiostreamincludemapintmain(){std::multimapchar,intmymm;mymm。insert(std::makepair(x,10));mymm。insert(std::makepair(y,20));mymm。insert(std::makepair(z,30));mymm。insert(std::makepair(z,40));std::multimapchar,int::iteratoritmymm。find(x);mymm。erase(it);mymm。erase(mymm。find(z));printcontent:std::coutelementsinmymm:;std::coutymymm。find(y)second;std::coutzmymm。find(z)second;return0;}Output:elementsinmymm:y20z402multimap的多值查找
2。1使用成员函数lowerbound()和upperbound()iteratorlowerbound(constkeytypek);constiteratorlowerbound(constkeytypek)const;iteratorupperbound(constkeytypek);constiteratorupperbound(constkeytypek)const;
demo:includeiostreamincludemapintmain(){std::multimapchar,intmymultimap;std::multimapchar,int::iteratorit,itlow,itup;mymultimap。insert(std::makepair(a,10));mymultimap。insert(std::makepair(b,121));mymultimap。insert(std::makepair(c,1001));mymultimap。insert(std::makepair(c,2002));mymultimap。insert(std::makepair(d,11011));mymultimap。insert(std::makepair(e,44));itlowmymultimap。lowerbound(c);itlowpointstobitupmymultimap。upperbound(c);ituppointstoe(notd)printrange〔itlow,itup):for(ititlow;it!itup;it)std::cout(it)。first(it)。second;getchar();return0;}output:c1001c2002
2。2使用成员函数equalrange()templateclassForwardIterator,classTpairForwardIterator,ForwardIteratorequalrange(ForwardIteratorfirst,ForwardIteratorlast,constTval){ForwardIteratoritstd::lowerbound(first,last,val);returnstd::makepair(it,std::upperbound(it,last,val));}
equalrange()返回一对起始位置和终点位置的迭代器(pair类型),可以使用一个迭代器指向返回的pair的first和second。Associativecontainerfunctionequalrange()returnsapaircontainingtheresultsalowerboundandanupperboundoperation。includeiostreamincludemapintmain(){std::multimapchar,intmymm;mymm。insert(std::makepair(x,50));mymm。insert(std::makepair(y,100));mymm。insert(std::makepair(y,150));mymm。insert(std::makepair(y,200));mymm。insert(std::makepair(z,250));mymm。insert(std::makepair(z,300));for(charcx;cz;c){std::coutTherearemymm。count(c)elementswithkeyc:;std::multimapchar,int::iteratorit;for(itmymm。equalrange(c)。first;it!mymm。equalrange(c)。second;it)std::cout(it)。second;std::cout;}getchar();return0;}Output:Thereare1elementswithkeyx:50Thereare3elementswithkeyy:100150200Thereare2elementswithkeyz:250300
另一种写法:includeiostreamincludemapintmain(){std::multimapchar,intmymm;mymm。insert(std::pairchar,int(a,10));mymm。insert(std::pairchar,int(b,20));mymm。insert(std::pairchar,int(b,30));mymm。insert(std::pairchar,int(b,40));mymm。insert(std::pairchar,int(c,50));mymm。insert(std::pairchar,int(c,60));mymm。insert(std::pairchar,int(d,60));std::coutmymmcontains:;for(charcha;chd;ch){std::pairstd::multimapchar,int::iterator,std::multimapchar,int::iteratorret;retmymm。equalrange(ch);std::coutch;for(std::multimapchar,int::iteratoritret。first;it!ret。second;it)std::coutitsecond;std::cout;}getchar();return0;}output:mymmcontains:a10b203040c5060d603std::map::findcountiteratorfind(constkeytypek);constiteratorfind(constkeytypek)const;sizetypecount(constkeytypek)const;
demo:includeiostreamincludemapintmain(){std::mapchar,intmymap;std::mapchar,int::iteratorit;mymap〔a〕50;mymap〔b〕100;mymap〔c〕150;mymap〔d〕200;itmymap。find(b);if(it!mymap。end())mymap。erase(it);printcontent:std::coutelementsinmymap:;std::coutamymap。find(a)second;std::coutcmymap。find(c)second;std::coutdmymap。find(d)second;if(mymap。count(e)0)std::coutemymap。find(e)second;elsestd::coutenotexist!;getchar();return0;}output:elementsinmymap:a50c150d200enotexist!4std::map::equalrangepairconstiterator,constiteratorequalrange(constkeytypek)const;pairiterator,iteratorequalrange(constkeytypek);
demo:includeiostreamincludemapintmain(){std::mapchar,intmymap;mymap〔a〕10;mymap〔b〕20;mymap〔c〕30;std::pairstd::mapchar,int::iterator,std::mapchar,int::iteratorret;retmymap。equalrange(b);std::coutlowerboundpointsto:;std::coutret。firstfirstret。firstsecond;std::coutupperboundpointsto:;std::coutret。secondfirstret。secondsecond;return0;}output:lowerboundpointsto:b20upperboundpointsto:c305std::map::lowerboundupperbounditeratorlowerbound(constkeytypek);constiteratorlowerbound(constkeytypek)const;iteratorupperbound(constkeytypek);constiteratorupperbound(constkeytypek)const;
demo:includeiostreamincludemapintmain(){std::mapchar,intmymap;std::mapchar,int::iteratoritlow,itup;mymap〔a〕20;mymap〔b〕40;mymap〔c〕60;mymap〔d〕80;mymap〔e〕100;itlowmymap。lowerbound(b);itlowpointstobitupmymap。upperbound(d);ituppointstoe(notd!)mymap。erase(itlow,itup);erases〔itlow,itup)printcontent:for(std::mapchar,int::iteratoritmymap。begin();it!mymap。end();it)std::coutitfirstitsecond;return0;}output:a20e1006std::map::at(C11)
成员函数at有边界检查,当然也有性能损耗。mappedtypeat(constkeytypek);constmappedtypeat(constkeytypek)const;
demo:includeiostreamincludestringincludemapintmain(){std::mapstd::string,intmymap{{alpha,0},{beta,0},{gamma,0}};mymap。at(alpha)10;mymap。at(beta)20;mymap。at(gamma)30;for(autox:mymap){std::coutx。first:x。second;}std::coutmymap。at(alpha);std::coutmymap。at(empty);std::coutmymap。at(alpha);return0;}output:alpha:10beta:20gamma:3010terminatecalledafterthrowinganinstanceofstd::outofrangewhat():map::at7std::map::operator〔〕
重载操作符operator〔〕没有边界检查,当然也就避免了性能损耗。mappedtypeoperator〔〕(constkeytypek);
demo:includeiostreamincludestringincludemapintmain(){std::mapstd::string,intmymap{{alpha,0},{beta,0},{gamma,0}};mymap〔alpha〕10;mymap〔beta〕20;mymap〔gamma〕30;for(autox:mymap){std::coutx。first:x。second;}std::coutmymap〔alpha〕;std::coutmymap〔empty〕;0return0;}output:alpha:10beta:20gamma:30100
ref
http:www。cplusplus。comreferencemapmultimapcount
http:www。cplusplus。comreferencemapmultimapequalrange
瞿颖李亚鹏为什么分手被插足横刀夺爱是真的吗瞿颖和李亚鹏当年的感情那么好,到最后他们俩为什么分的手大家知道吗?李亚鹏虽说现在已经退圈了,可他人虽不在娱乐圈,娱乐圈却依然有关于他的传说,尤其是感情方面的,谁让李亚鹏出道后与……
侯天旭余艺两人关系优衣库里面提到的两个人侯天旭,余艺的名字能够跟众多明星并列在一起出现在网络上,那是因为他们两位在三里屯的优衣库试衣间里面做了一件让数以万记的网友们都瞠目结舌的事情:当时有人在这个地方的试衣间里面做运……
183club为什么会解散谁是事件的导火索台湾曾经著名的组合183club。在台湾和内地迅速的走红。不管是组合出的音乐转接,还是成员们一起演出的偶像剧。在零几年的时候是相当火爆的。不过这个组合的发展并没有维持几年。组合……
国家要开始收拾刘强东?外有舆论内有施压扛不住了刘强东之前是京东网店的大老板,他生活在媒体的聚光灯下,无数的眼睛都盯着他的一举一动,他和明星的生活其实差不多的,最近你有没有发现刘强东前后已经卸任了几十家企业高层的职务,不少网……
李明霖结婚了吗自己说在35岁之前一定要结婚不少的人其实都比较喜欢长得帅的明星,但是了这是任何人的都喜欢美丽的事物的本质,所以李明霖一出道就非常的受欢迎了,很多人都感觉李明霖长得真的是很帅气的,有不少的人还说李明霖其实是……
家用中央空调如何清洗,需要注意些什么?中央空调有多种机型的,一般分为家用中央空调,商用中央空调,集中式中央空调,大型冷水机等,下面我们就说说家用中央空调的清洗方法。家用中央空调分为风管机,多联机二种,着重就说……
Linux指令学习3自己学习总结文档,有些乱勿怪注意linux指令的大小写1、tar,tar将压缩和解压缩集合在一起,使用不同的参数即可,命令格式如下:tar〔参数〕〔压缩……
瞿颖挂历事件照片早年走红的象征如今反被嘲讽瞿颖挂历事件的照片现在还有吗?现在的年轻人别说知道这件事情了,恐怕连瞿颖是谁都不知道吧,说起来最开始瞿颖也是非常火的模特明星,挂历事件在那个年代本来是走红的象征,没想到如今却被……
王力宏为什么叫二哥,王力宏二哥的由来在娱乐圈中有不少明星有外号,成龙大哥是因为他的作品成就和为中国文艺界做出的贡献,二姐张歆艺的因自身比较二,又大大咧咧和有点糊涂。可华语乐坛才子王力宏有个外号叫二哥,这是怎么回事……
章泽天父亲是南京首富不依附刘强东依然是个出色的女人当年奶茶妹妹章泽天和京东掌门人刘强东恋情曝光的时候,很多人因为他们巨大的年轻差而惊讶。不少人认为章泽天之所以选择和大自己那么多的刘强东在一起,最主要是因为刘强东是在商场上是个身……
给力!中国银行宣布10万亿元支持各地产业链延链补链河北资本近日,中国银行发布了《支持产业链供应链现代化水平提升的十五条措施》,旨在支持重点产业集群延链补链强链固链,未来五年为供应链核心企业提供超过10万亿元的流动性支持,……
重庆红衣小男孩事件破案了吗11年都没有破案之后更难了虽然如今的科技水平在不断的发展,但是有很多事情终究还是无法给出答案的,比如说之前的一些看起来非常悬疑的案件,至今也没有办法找到所谓的真正的答案,比如说醉卧著名的重庆红衣小男孩的……