信用 网站 建设方案,wordpress多个内容模块,网站开发怎么切换多种语言,定制网站开发方案C11中Map的使用Map是c的一个标准容器#xff0c;她提供了很好一对一的关系#xff0c;在一些程序中建立一个map可以起到事半功倍的效果#xff0c;总结了一些map基本简单实用的操作#xff01;1. map最基本的构造函数#xff1b;mapstring , int mapstring; map11中Map的使用Map是c的一个标准容器她提供了很好一对一的关系在一些程序中建立一个map可以起到事半功倍的效果总结了一些map基本简单实用的操作1. map最基本的构造函数mapstring , int mapstring; mapint ,string mapint;mapsring, charmapstring; map char ,stringmapchar;mapchar ,intmapchar; mapint ,char mapint2. map添加数据mapint ,string maplive;1.maplive.insert(pairint,string(102,“aclive”));2.maplive.insert(mapint,string::value_type(321,“hai”));3, maplive[112]“April”;//map中最简单最常用的插入添加3map中元素的查找find()函数返回一个迭代器指向键值为key的元素如果没找到就返回指向map尾部的迭代器。mapint ,string ::iterator l_it;;l_itmaplive.find(112);if(l_itmaplive.end())cout“we do not find 112”endl;else cout“wo find 112”endl;4,map中元素的删除如果删除112mapint ,string ::iterator l_it;;l_itmaplive.find(112);if(l_itmaplive.end())cout“we do not find 112”endl;else maplive.erase(l_it); //delete 112;5,map中 swap的用法Map中的swap不是一个容器中的元素交换而是两个容器交换For example #include map #include iostream using namespace std; int main( ) { map int, int m1, m2, m3; map int, int::iterator m1_Iter; m1.insert ( pair int, int ( 1, 10 ) ); m1.insert ( pair int, int ( 2, 20 ) ); m1.insert ( pair int, int ( 3, 30 ) ); m2.insert ( pair int, int ( 10, 100 ) ); m2.insert ( pair int, int ( 20, 200 ) ); m3.insert ( pair int, int ( 30, 300 ) ); cout The original map m1 is:; for ( m1_Iter m1.begin( ); m1_Iter ! m1.end( ); m1_Iter ) cout m1_Iter-second; cout . endl; // This is the member function version of swap //m2 is said to be the argument map; m1 the target map m1.swap( m2 ); cout After swapping with m2, map m1 is:; for ( m1_Iter m1.begin( ); m1_Iter ! m1.end( ); m1_Iter ) cout m1_Iter - second; cout . endl; cout After swapping with m2, map m2 is:; for ( m1_Iter m2.begin( ); m1_Iter ! m2.end( ); m1_Iter ) cout m1_Iter - second; cout . endl; // This is the specialized template version of swap swap( m1, m3 ); cout After swapping with m3, map m1 is:; for ( m1_Iter m1.begin( ); m1_Iter ! m1.end( ); m1_Iter ) cout m1_Iter - second; cout . endl;}6.map的sort问题Map中的元素是自动按key升序排序,所以不能对map用sort函数For example #include map #include iostream using namespace std; int main( ) { map int, int m1; map int, int::iterator m1_Iter; m1.insert ( pair int, int ( 1, 20 ) ); m1.insert ( pair int, int ( 4, 40 ) ); m1.insert ( pair int, int ( 3, 60 ) ); m1.insert ( pair int, int ( 2, 50 ) ); m1.insert ( pair int, int ( 6, 40 ) ); m1.insert ( pair int, int ( 7, 30 ) ); cout The original map m1 is:endl; for ( m1_Iter m1.begin( ); m1_Iter ! m1.end( ); m1_Iter ) cout m1_Iter-first m1_Iter-secondendl; } The original map m1 is: 1 20 2 50 3 60 4 40 6 40 7 30 请按任意键继续. . .map的基本操作函数C Maps是一种关联式容器包含“关键字/值”对begin() 返回指向map头部的迭代器clear( 删除所有元素count() 返回指定元素出现的次数empty() 如果map为空则返回trueend() 返回指向map末尾的迭代器equal_range() 返回特殊条目的迭代器对erase() 删除一个元素find() 查找一个元素get_allocator() 返回map的配置器insert() 插入元素key_comp() 返回比较元素key的函数lower_bound() 返回键值给定元素的第一个位置max_size() 返回可以容纳的最大元素个数rbegin() 返回一个指向map尾部的逆向迭代器rend() 返回一个指向map头部的逆向迭代器size() 返回map中元素的个数swap() 交换两个mapupper_bound() 返回键值给定元素的第一个位置value_comp() 返回比较元素value的函数补map中迭代器的访问问题for(register mapint,int::iterator im.begin();i!m.end();i){pairint,intit*i;coutit.first it.secondendl;
}
1234与下面实现的功能相同mapint, string::iterator iter;
for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {coutiter-first iter-secondendl;
}
1234迭代器 iter返回的是一个地址iter-first是指针指向的第一个元素(*iter).first则表示地址里的pair结构中的第一个数据附上string相关string string的若干初始化方法string test1; //空串string test2 “内容”; //使用string test3(“内容”); //使用引用字符数组作为参数传给构造函数string test4(test2); //用一个string初始化另一而stringstring test5(test2,pos,num); //从test2中的第pos个位置开始拷贝个数为num个字符string test6 test2 “内容” test3 //混合初始化string test7 test2.substr(pos,num); //从test2中的第pos个位置开始拷贝个数为num个字符string test8 test2.substr(); //参数列表为空则会拷贝test2的整个对象复制test2的简便方法string test9(num,ch); //拷贝num个字符型ch到test
文章转载自: http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.iqcge.com.gov.cn.iqcge.com http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn