锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Map鏄爣鍑?strong>鍏寵仈寮忓鍣?/strong>錛?em>associative container錛変箣涓錛屼竴涓猰ap鏄竴涓敭鍊煎搴忓垪錛屽嵆錛坘ey ,value錛夊銆傚畠鎻愪緵鍩轟簬key鐨?strong>蹇?/strong>媯绱㈣兘鍔涳紝鍦ㄤ竴涓猰ap涓璳ey鍊兼槸鍞竴鐨勩俶ap鎻愪緵鍙屽悜榪唬鍣紝鍗蟲湁浠庡墠寰鍚庣殑錛坕terator錛夛紝涔熸湁浠庡悗寰鍓嶇殑錛坮everse_iterator錛夈?/p>
map瑕佹眰鑳藉key榪涜<鎿嶄綔錛屼笖淇濇寔鎸塳ey鍊奸掑鏈夊簭錛屽洜姝ap涓婄殑榪唬鍣ㄤ篃鏄掑鏈夊簭鐨勩傚鏋滃浜庡厓绱犲茍涓嶉渶瑕佷繚鎸佹湁搴忥紝鍙互浣跨敤hash_map銆?/p>
map涓璳ey鍊兼槸鍞竴鐨勶紝濡傛灉椹尮涓凡瀛樺湪涓涓敭鍊煎(鏄電О,瀵嗙爜):("skynet",407574364)錛岃屾垜浠繕鎯蟲彃鍏ヤ竴涓敭鍊煎("skynet",472687789)鍒欎細鎶ラ敊錛?span style="color: #ff0000;">涓嶆槸鎶ラ敊錛屽噯紜殑璇存槸錛岃繑鍥炴彃鍏ヤ笉鎴愬姛錛?/span>錛夈傝屾垜浠張鐨勭‘鎯寵繖鏍峰仛錛屽嵆涓涓敭瀵瑰簲澶氫釜鍊鹼紝騫歌繍鐨勬槸multimap鍙槸瀹炵幇榪欎釜鍔熻兘銆?/p>
涓嬮潰鎴戜滑鐢ㄥ疄渚嬫潵娣卞叆浠嬬粛map銆?em>multimap錛屼富瑕佸唴瀹瑰涓嬶細 鏈変竴涓湇鍔″櫒manager緇存姢鐫鎺ュ叆鏈嶅姟鍣ㄧ殑client淇℃伅錛屽寘鎷琧linetId銆乻canRate銆乻ocketAddr絳夌瓑銆傛垜浠畾涔変竴涓粨鏋勪綋淇濆瓨scanRate銆乻ocketAddr淇℃伅銆傚涓嬶細 鎴戜滑鐢╩ap淇濆瓨榪欎簺淇℃伅錛歝lientId涓洪敭key錛宑lientInfo涓哄箋傝繖鏍鋒垜浠彲浠ラ氳繃clientId蹇熸绱㈠埌client鐨勭浉鍏充俊鎭紝鎴戜滑鍙互榪欐牱瀹氫箟錛?/p>
榪欐牱鎴戜滑瀹氫箟浜嗕竴涓猚lientMap錛屽鏋滄垜浠瀹氫箟澶氫釜榪欐牱鐨刴ap錛岄渶瑕佸嬈″啓map<clientId,clientInfo> 鍙橀噺鍚嶃備負浜嗛伩鍏嶈繖鏍鋒儏鍐碉紝鎴戜滑閫氬父涓簃ap<clientId,clientInfo>瀹氫箟涓埆鍚嶏紝濡傦細 涔嬪悗鎴戜滑灝卞彲浠ュ儚瀹氫箟clientMap涓鏍峰畾涔塵ap<clientId,clientInfo>瀵硅薄錛岃繖鏍風殑濂藉榪樻湁錛氬鏋滄垜浠渶瑕佷慨鏀筸ap鐨勫畾涔夛紝鍙渶瑕佸湪涓澶勪慨鏀瑰嵆鍙紝閬垮厤淇敼涓嶅交搴曢犳垚鐨勪笉涓鑷寸幇璞°?/p>
鎴戜滑榪欏氨瀹屾垚浜嗛渶瑕佺殑map鐨勫畾涔夛紝濡傛灉涓嶅畾涔夋垨娌℃湁鍦ㄥ畠涓婇潰鐨勬搷浣滅殑璇濓紝灝卞儚瀹氫箟綾昏屾病鏈夋柟娉曚竴鏍鳳紝鎰忎箟涓嶅ぇ鎴栨鏃犳剰涔夈傚垢榪愮殑鏄紝STL鎻愪緵浜嗚繖浜涘父鐢ㄦ搷浣滐細鎺掑簭錛堟敞錛歮ap鏄笉鑳戒篃涓嶈鎺掑簭鐨勶紝鍥犱負map鏈韓宸茬粡鎺掑ソ搴忎簡錛夈佹墦鍗般佹彁鍙栧瓙閮ㄥ垎銆佺Щ闄ゅ厓绱犮佹坊鍔犲厓绱犮佹煡鎵懼璞★紝灝卞儚鏁版嵁搴撶殑澧炲垹鏀規煡鎿嶄綔錛佺幇鍦ㄦ垜浠緇嗕粙緇嶈繖浜涙搷浣滐紝騫墮愭寮曞叆hash_map銆?em>multimap銆?/p>
鍏寵仈鏁扮粍錛?em>associative array錛夋槸鏈鏈夌敤鐨勭敤鎴峰畾涔夌被鍨嬩箣涓錛岀粡甯稿唴緗湪璇█涓敤浜庢枃鏈鐞嗙瓑銆備竴涓叧鑱旀暟緇勯氬父涔熺О涓簃ap錛屾湁鏃朵篃縐板瓧鍏革紙dictionary錛夛紝淇濆瓨涓瀵瑰箋傜涓涓肩О涓簁ey銆佺浜屼釜縐頒負鏄犲皠鍊糾apped-value銆?/p>
鏍囧噯map鏄畾涔夊湪std鍛藉悕絀洪棿涓殑涓涓ā鏉匡紝騫惰〃紺轟負<map>銆傚畠棣栧厛瀹氫箟浜嗕竴緇勬爣鍑嗙被鍨嬪悕瀛楋細 娉ㄦ剰錛歮ap鐨剉alue_type鏄竴涓?key,value)瀵癸紝鏄犲皠鍊肩殑琚涓烘槸mapped_type銆傚洜姝わ紝涓涓猰ap鏄竴涓猵air<const Key,mapped_type>鍏冪礌鐨勫簭鍒椼備粠const Key鍙互鐪嬪嚭錛宮ap涓敭key鏄笉鍙慨鏀圭殑銆?/p>
涓嶅緱涓嶆彁鐨勬槸map瀹氫箟涓瑿mp鍜孉閮芥槸鍙夐」銆侰mp鏄畾涔夊湪鍏冪礌涔嬮棿鐨勬瘮杈冩柟娉曪紝榛樿鏄?lt;鎿嶄綔錛汚鍗砤llocator鐢ㄦ潵鍒嗛厤鍜?strong>閲婃斁map鎬婚敭鍊煎鎵闇浣跨敤鐨勫唴瀛橈紝娌℃湁鎸囧畾鐨勮瘽鍗抽粯璁や嬌鐢ㄧ殑鏄疭TL鎻愪緵鐨勶紝涔熷彲浠ヨ嚜瀹氫箟allocator鏉ョ鐞嗗唴瀛樼殑浣跨敤銆傚鏁版儏鍐碉紝鎴戜滑涓嶆寚瀹氳繖涓や釜閫夐」鑰屼嬌鐢ㄩ粯璁ゅ鹼紝榪欐牱鎴戜滑瀹氫箟map灝卞儚涓嬮潰榪欐牱錛?/p>
Cmp鍜孉閮界己鐪併?閫氬父錛屽疄闄呯殑榪唬鍣ㄦ槸瀹炵幇瀹氫箟鐨勶紝鍥犱負map寰堝儚浣跨敤浜嗘爲鐨勫艦寮忥紝榪欎簺榪唬鍣ㄩ氬父鎻愪緵鏍戦亶鍘嗙殑鏌愮褰㈠紡銆傞嗗悜榪唬鍣ㄦ槸浣跨敤鏍囧噯鐨剅everse_iterator妯℃澘鏋勯犵殑銆?/p>
map鎻愪緵鎯父鐨勮繑鍥炶凱浠e櫒鐨勪竴緇勫嚱鏁幫紝濡備笅鎵紺猴細 map涓婄殑榪唬鍣ㄦ槸pair<const Key,mapped_type>鍏冪礌搴忓垪涓婄畝鍗曠殑榪唬銆備緥濡傦紝鎴戜滑鍙兘闇瑕佹墦鍗板嚭鎵鏈夌殑瀹㈡埛绔俊鎭紝鍍忎笅闈㈢殑紼嬪簭榪欐牱銆備負浜嗗疄鐜拌繖涓紝鎴戜滑棣栧厛鍚戙婁緥瀛愬紩鍏ャ嬩腑瀹氫箟鐨刢lientEdp涓彃鍏ユ暟鎹紝鐒跺悗鎵撳嵃鍑烘潵錛?/p>
涓涓猰ap榪唬鍣ㄤ互key鍗囧簭鏂瑰紡琛ㄧず鍏冪礌錛屽洜姝ゅ鎴風淇℃伅浠liendId鍗囧簭鐨勬柟寮忚緭鍑恒傝繍琛岀粨鏋滃彲浠ヨ瘉鏄庤繖涓鐐癸紝榪愯緇撴灉濡備笅鎵紺猴細 鍥?銆佺▼搴忚繍琛岀粨鏋?/p>
鎴戜滑浠irst寮曠敤閿煎鐨刱ey錛屼互second寮曠敤mapped value錛屼笖涓嶇敤綆ey鍜宮apped value鏄粈涔堢被鍨嬨傚叾瀹瀙air鍦╯td鐨勬ā鏉夸腑鏄繖鏍峰畾涔夌殑錛?/p>
鍗砿ap涓紝key鏄敭鍊煎鐨勭涓涓厓绱犱笖mapped value鏄浜屼釜鍏冪礌銆俻air鐨勫畾涔夊彲浠ュ湪<utility>涓壘鍒幫紝pair鎻愪緵浜嗕竴涓柟娉曟柟渚垮垱寤洪敭鍊煎錛?/p>
涓婇潰鐨勪緥瀛愪腑鎴戜滑灝辯敤鍒頒簡榪欎釜鏂規硶鏉ュ垱寤?clientId,clientInfo)瀵癸紝騫朵綔涓篒nsert()鏂規硶鐨勫弬鏁般傛瘡涓猵air榛樿鍒濆鍖栨瘡涓厓绱犵殑鍊間負瀵瑰簲綾誨瀷鐨勯粯璁ゅ箋?/p>
map綾繪儻甯告彁渚涗簡鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟錛屽涓嬫墍紺猴細 澶嶅埗涓涓鍣ㄦ剰鍛崇潃涓哄畠鐨勬瘡涓厓绱犲垎閰嶇┖闂達紝騫舵嫹璐濇瘡涓厓绱犲箋傝繖鏍峰仛鏄ц兘寮閿鏄緢澶х殑錛屽簲璇ヤ粎褰撻渶瑕佺殑鏃跺欐墠榪欐牱鍋氥?strong>鍥犳錛宮ap浼犵殑鏄紩鐢?/strong>銆?/p>
鍓嶉潰鎴戜滑宸茬粡璇磋繃錛屽鏋渕ap涓粎瀹氫箟浜嗕竴浜沰ey銆乵apped value綾誨瀷鐨勪俊鎭屾病鏈夋搷浣滄柟娉曪紝灝卞瀹氫箟涓粎鏈夊瓧孌電殑綾繪剰涔変笉澶х敋鑷蟲鏃犳剰涔夈傜敱姝ゅ彲瑙乵ap涓畾涔夋搷浣滄柟娉曢潪甯擱噸瑕侊紒鍓嶉潰鐨勪緥瀛愭垜浠氨鐢ㄥ埌浜嗕笉灝戞柟娉曪紝濡傝繑鍥炶凱浠e櫒鐨勬柟娉昩egin()銆乪nd()錛岄敭鍊煎鎻掑叆鏂規硶insert()銆備笅闈㈡垜浠map涓殑鎿嶄綔鏂規硶鍋氫釜鍏ㄩ潰鐨勪粙緇嶏細 涓婇潰榪欎簺鏂規硶鍩烘湰閮借兘欏懼悕鎬濅箟錛圥S.鐢辨鍙錛屽懡鍚嶆湁澶氶噸瑕侊紝鎴戜滑騫蟲椂瑕佸吇鎴愬ソ鐨勫懡鍚嶄範鎯紝褰撶劧娉ㄩ噴涔熷繀涓嶅彲灝戯紒錛夈傝櫧鐒跺凡緇忛潪甯告竻妤氫簡浜嗭紝浣嗘垜榪樻槸鎯寵瑙d竴涓嬩互娑堥櫎涓嶆儨瑕佺殑璇В鍜屾洿濂藉湴搴旂敤榪欎簺鏂規硶銆?/p>
涓婇潰鎴戜滑鐪嬪嚭錛岃繖閲屾垜浠彃鍏ラ敭鍊煎鏄鍏堝0鏄庝竴涓敭鍊煎pair<clientId,clientInfo> p110(id,cltInfo); 鐒跺悗鍐嶆彃鍏ワ紝榪欎釜鎴戜滑涔嬪墠make_pair鏂規硶涓嶄竴鏍鳳紝make_pair鏂規硶鐢ㄧ殑姣旇緝澶氥?/p>
鍓嶉潰鎴戜滑浠嬬粛浜嗗埄鐢╩ap鐨勬彃鍏ユ柟娉昳nsert()錛屽0鏄庨敭鍊煎pair鎴杕ake_pair鐢熸垚閿煎鐒跺悗鎴戜滑鍙互杞繪澗鐨勫皢閿煎鎻掑叆map涓傚叾瀹瀖ap榪樻彁渚涗簡鏇存柟渚跨殑鎻掑叆鎿嶄綔鍒╃敤涓嬫爣錛坰ubscripting錛孾]錛夋搷浣滐紝濡備笅錛?/p>
榪欐牱鎴戜滑灝卞彲浠ョ畝鍗曞湴灝嗛敭鍊煎鎻掑叆鍒癿ap涓簡銆備笅鏍囨搷浣滃湪map涓紡榪欐牱瀹氫箟鐨勶細 鎴戜滑鏉ュ垎鏋愪竴涓嬪簲鐢╗]鎿嶄綔錛屾彃鍏ラ敭鍊煎鐨勮繃紼嬶細媯鏌ラ敭k鏄惁宸茬粡鍦╩ap閲屻傚鏋滀笉錛屽氨娣誨姞涓婏紝浠浣滀負瀹冪殑瀵瑰簲鍊箋傚鏋渒宸茬粡鍦╩ap閲岋紝瀹冪殑鍏寵仈鍊艱鏇存柊鎴恦銆傝繖閲岄鍏堬紝鏌ユ壘110涓嶅湪map涓垯鍒涘緩涓涓敭涓?10鐨勯敭鍊煎錛屽茍灝嗘槧灝勫艱涓洪粯璁ゅ鹼紝榪欓噷scanRate涓?錛宻ocketAddr涓虹┖錛涚劧鍚庡皢鏄犲皠鍊艱祴涓篶ltInfo銆?濡傛灉110鍦╩ap涓凡緇忓瓨鍦ㄧ殑璇濓紝鍒欏彧鏄洿鏂頒互110涓洪敭鐨勬槧灝勫箋?/p>
浠庝笂闈㈢殑鍒嗘瀽鍙煡錛氬鏋滃ぇ閲忚繖鏍鋒彃鍏ユ暟鎹紝浼氫弗閲嶅獎鍝嶆晥鐜囷紒濡傛灉浣犺冭檻鏁堢巼闂錛岃浣跨敤insert鎿嶄綔銆俰nsert鏂規硶錛岃妭鐪佷簡涓夋鍑芥暟璋冪敤錛氫竴涓緩绔嬩復鏃剁殑榛樿鏄犲皠鍊肩殑瀵硅薄錛屼竴涓攢姣侀偅涓復鏃剁殑瀵硅薄鍜屼竴涓鏄犲皠鍊肩殑璧嬪兼搷浣溿?/p>
Note1錛?/strong>濡傛灉k宸茬粡瀛樺湪map涓紝[]鏁堢巼鍙嶈屾瘮insert鐨勬晥鐜囬珮錛岃屼笖鏇寸編瑙傦紒濡傛灉鑳藉鍏奸【榪欎袱鑰呴偅宀備笉鏄緢緹庡錛佸叾瀹炴垜浠噸鍐檓ap涓殑[]鎿嶄綔錛氶鍏堝垽鏂璳鏄惁宸茬粡鍦╩ap涓紝濡傛灉娌℃湁鍒欒皟鐢╥nsert鎿嶄綔錛屽惁鍒欒皟鐢ㄥ唴緗殑[]鎿嶄綔銆傚涓嬪垪浠g爜錛?/p>
Note2錛?/strong>鎴戜滑瑙嗕箮榪樺拷鐣ヤ簡涓鐐癸紝濡傛灉鏄犲皠鍊糾apped value鐨勭被鍨嬫病鏈夐粯璁ゅ鹼紝鎬庝箞鍔烇紵榪欑鎯呭喌璇峰嬁浣跨敤[]鎿嶄綔鎻掑叆銆?/p>
閫氳繃[]鎿嶄綔涓嶄粎浠呮槸鎻掑叆閿煎錛屾垜浠篃鍙互閫氳繃閿甼ey媯绱㈠嚭鏄犲皠鍊糾apped value銆傝屼笖鎴戜滑鍒╃敤[]鎿嶄綔鍙互杞繪澗鍦扮粺璁′俊鎭紝濡傛湁榪欐牱榪欐牱涓浜涢敭鍊煎錛坆ook-name錛宑ount錛夊錛?/p>
(book1,1)銆?book2,2)銆?book1,2)銆?book3,1)銆?book3,5) 鎴戜滑璁$畻姣忕book鐨勬暟閲忔誨拰銆傛垜浠彲浠ヨ繖鏍峰仛錛氬皢瀹冧滑璇誨叆涓涓猰ap<string,int>錛?/p>
緇撴灉濡備笅鎵紺猴細錛堟敞鎰忔寜浣廲trl+z閿粨鏉熻緭鍏ワ級 鍥?銆佺▼搴忚繍琛岀粨鏋?/p>
鍓嶉潰浠嬬粛浜唌ap錛屽彲浠ヨ宸茬粡闈炲父娓呮櫚浜嗐傚鏋滃厑璁竎lientId閲嶅鐨勮瘽錛宮ap灝辨棤鑳戒負鍔涗簡錛岃繖鏃跺欏氨寰梞ultimap涓婂満浜嗭紒multimap鍏佽閿甼ey閲嶅錛屽嵆涓涓敭瀵瑰簲澶氫釜鏄犲皠鍊箋?/strong>鍏跺疄闄ゆ涔嬪錛宮ultimap璺焟ap鏄緢鍍忕殑錛屾垜浠帴涓嬫潵鍦╩ap鐨勫熀紜涓婁粙緇峬ultimap銆?/p>
multimap鍦╯td涓殑瀹氫箟璺焟ap涓鏍峰彧鏄被鍚嶄負multimap錛宮ultimap鍑犱箮鏈塵ap鐨勬墍鏈夋柟娉曞拰綾誨瀷瀹氫箟銆?/p>
鍋囪鎴戜滑鎯沖彇鍑洪敭涓簁ey鐨勬墍鏈夋槧灝勫鹼紝鎴戜滑鍙互榪欐牱鍋氾細 錛堣鏄庯細瀹為檯涓婏紝涓鑸槸涓嶅厑璁竎lientId閲嶅鐨勶紝榪欓噷鍙槸涓轟簡涓句緥銆傦級榪欐牱鏄笉鏄劅瑙夊緢涓戝憿錛佷簨瀹炰笂錛屾垜浠彲浠ユ洿綆鍗曠殑榪欐牱錛?/p>
map鏄竴綾誨叧鑱斿紡瀹瑰櫒銆傚畠鐨勭壒鐐規槸澧炲姞鍜屽垹闄よ妭鐐瑰榪唬鍣ㄧ殑褰卞搷寰堝皬錛岄櫎浜嗛偅涓搷浣滆妭鐐癸紝瀵瑰叾浠栫殑鑺傜偣閮芥病鏈変粈涔堝獎鍝嶃傚浜庤凱浠e櫒鏉ヨ錛屽彲浠ヤ慨鏀瑰疄鍊鹼紝鑰屼笉鑳戒慨鏀筴ey銆? map鐨勫姛鑳斤細 灞曟湜錛氭湰鏂囦笉鐭ヤ笉瑙夊啓浜嗕笉灝戝瓧浜嗭紝浣嗕粛鏈繁鍏ユ秹鍙婂埌map瀹氫箟鐨勭3涓拰絎?涓弬鏁幫紝浣跨敤鐨勯兘鏄粯璁ゅ箋?/p>
template<class Key,class T,class Cmp=less<key>, 鎰熷叴瓚h咃紝璇鋒煡鎵劇浉鍏寵祫鏂檕r涓嬮潰鐣欒█甯屾湜鐪嬪埌鍗曠嫭寮綃囦粙緇峬ap絎?涓拰絎?涓弬鏁般傛偍鐨勬敮鎸侊紝鎴戠殑鍔ㄥ姏錛丳S錛氬湪姝ゆ枃鐨勫師鍥狅紝鍦ㄤ笌鍏徃鍋氶」鐩敤鍒頒簡map鐗規鎬葷粨鍑烘潵涓庡ぇ瀹跺叡浜紝涓嶈繃鍦ㄨ繘琛屼釜浜烘葷粨榪囩▼涓紝闅懼厤浼氭湁鐤忔紡鎴栦笉褰撲箣澶勶紝璇蜂笉鍚濇寚鍑恒?/p>
銆?銆戙奣he C++ Programming Language (Special Edition)銆?/p>
銆?銆戙奅ffective STL銆?/p>
1銆佷緥瀛愬紩鍏?/strong>
typedef int clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;
map<clientId,clientInfo> clientMap;
typedef map<clientId,clientInfo> clientEdp;
clientEdp clientMap;
2銆乵ap涓殑綾誨瀷瀹氫箟
template<class Key,class T,class Cmp=less<key>,
class A=allocator<pair<const Key,T>>
class std::map
{
public:
//types
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key,T> value_type;
typedef Cmp key_compare;
typedef A allocator_type;
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef implementation_define1 iterator;
typedef implementation_define2 const_iterator;
typedef typename A::size_type size_type;
typedef typename A::difference_type difference_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
//...
}
map<int,clientInfo> clientMap;
3銆乵ap涓殑榪唬鍣ㄥ拰閿煎
template<class Key,class T,class Cmp=less<key>,
class A=allocator<pair<const Key,T>>
class std::map
{
public:
//...
//iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
//...
}
#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef int clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;
int main(int argc,char** argv)
{
typedef map<clientId,clientInfo> clientEdp;
typedef map<clientId,clientInfo>::const_iterator iterator;
clientEdp clients;
clientInfo client[100];
char str[10];
string strAddr("socket addr client ");
for(int i=0;i<100;i++)
{
client[i].scanRate=i+1;
//convert int to char*
itoa(i+1,str,10);
//concatenate strAddr and str
client[i].socketAddr=strAddr+str;
cout<<client[i].socketAddr<<endl;
clients.insert(
make_pair(i+1,client[i]));
}
delete str;
for(iterator i=clients.begin();i!=clients.end();i++)
{
cout<<"clientId:"<<i->first<<endl;
cout<<"scanRate:"<<i->second.scanRate<<endl;
cout<<"socketAddr:"<<i->second.socketAddr<<endl;
cout<<endl;
}
}
template <class T1,class T2>struct std::pair{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair():first(T1()),second(T2()){}
pair(const T1& x,const T2& y):first(x),second(y){}
template<class U,class V>
pair(const pair<U,V>& p):first(p.first),second(p.second){}
}
template <class T1,class T2>pair<T1,T2>
std::make_pair(const T1& t1,const T2& t2)
{
return pair<T1,T2>(t1,t2);
}
4銆乵ap涓殑鏋勯犲嚱鏁頒笌鏋愭瀯鍑芥暟
template<class Key,class T,class Cmp=less<key>,
class A=allocator<pair<const Key,T>>
class std::map
{
//...
//construct/copy/destroy
explicit map(const Cmp&=Cmp(),const A&=A());
template<class In>map(In first,In last,
const Com&=Cmp(),const A&=A());
map(const map&);
~map();
map& operator=(const map&);
//...
}
5銆乵ap涓殑鎿嶄綔鏂規硶
template<class Key,class T,class Cmp=less<key>,
class A=allocator<pair<const Key,T>>
class std::map
{
//...
//map operations
//find element with key k
iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
//find number of elements with key k
size_type count() const;
//find first element with key k
iterator lower_bound(const key_type& k);
const_iterator lower_bound(const key_type& k) const;
//find first element with key greater than k
iterator upper_bound(const key_type& k);
const_iterator upper_bound(const key_type& k) const;
//insert pair(key,value)
pair<iterator,bool>insert(const value_type& val);
iterator insert(iterator pos,const value_type& val);
template<class In>void insert(In first,In last);
//erase element
void erase(iterator pos);
size_type erase(const key_type& k);
void erase(iterator first,iterator last);
void clear();
//number os elements
size_type size() const;
//size of largest possible map
size_type max_size() const;
bool empty() const{return size()==0;}
void swap(map&);
//...
}
#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef int clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;
int main(int argc,char** argv)
{
typedef map<clientId,clientInfo> clientEdp;
typedef map<clientId,clientInfo>::const_iterator iterator;
clientEdp clients;
clientInfo client[100];
char* str=new char[10];
string strAddr("socket addr client ");
for(int i=0;i<100;i++)
{
client[i].scanRate=i+1;
//convert int to char*
itoa(i+1,str,10);
//concatenate strAddr and str
client[i].socketAddr=strAddr+str;
clients.insert(
make_pair(i+1,client[i]));
}
delete str;
clientId id=10;
iterator i=clients.find(id);
if(i!=clients.end()){
cout<<"clientId: "<<id
<<" exists in clients"<<endl;
}
else{
cout<<"clientId: "<<id
<<" doesn't exist in clients"<<endl;
}
}
#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef int clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;
int main(int argc,char** argv)
{
typedef map<clientId,clientInfo> clientEdp;
typedef map<clientId,clientInfo>::const_iterator iterator;
clientEdp clients;
clientId id=110;
clientInfo cltInfo;
cltInfo.scanRate=10;
cltInfo.socketAddr="110";
pair<clientId,clientInfo> p110(id,cltInfo);
pair<iterator,bool> p=clients.insert(p110);
if(p.second){
cout<<"insert success!"<<endl;
}
else{
cout<<"insert failed!"<<endl;
}
//i points to clients[110];
iterator i=p.first;
cout<<i->first<<endl;
cout<<i->second.scanRate<<endl;
cout<<i->second.socketAddr<<endl;
}
6銆佸啀璁甿ap鐨勬彃鍏ユ搷浣?/strong>
clientInfo cltInfo;
cltInfo.scanRate=10;
cltInfo.socketAddr="110";
clients[110]=cltInfo;
template<class Key,class T,class Cmp=less<key>,
class A=allocator<pair<const Key,T>>
class std::map
{
//...
//access element with key k
mapped_type& operator[](const key_type& k);
//...
}
//////////////////////////////////////////////
///@param MapType-map鐨勭被鍨嬪弬鏁?
///@param KeyArgType-閿殑綾誨瀷鍙傛暟
///@param ValueArgtype-鏄犲皠鍊肩殑綾誨瀷鍙傛暟
///@return 榪唬鍣紝鎸囧悜閿負k鐨勯敭鍊煎
//////////////////////////////////////////////
template<typename MapType,
typename KeyArgType,
typename ValueArgtype>
typename MapType::iterator
efficientAddOrUpdate(MapType& m,
const KeyArgType& k,
const ValueArgtype& v)
{
typename MapType::iterator Ib = m.lower_bound(k);
if(Ib != m.end()&&!(m.key_comp()(k,Ib->first))) {
//key宸茬粡瀛樺湪浜巑ap涓仛鏇存柊鎿嶄綔
Ib->second = v;
return Ib;
}
else{
//key涓嶅瓨鍦╩ap涓仛鎻掑叆鎿嶄綔
typedef typename MapType::value_type MVT;
return m.insert(Ib, MVT(k, v));
}
}
7銆乕]涓嶄粎鎻掑叆
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(int argc,char** argv)
{
map<string,int> bookMap;
string book;
int count;
int total=0;
while(cin>>book>>count)
bookMap[book]+=count;
map<string,int>::iterator i;
for(i=bookMap.begin();i!=bookMap.end();i++)
{
total+=i->second;
cout<<i->first<<'\t'<<i->second<<endl;
}
cout<<"total count:"<<total<<endl;
}
8銆乵ultimap
pair<iterator,iterator> equal_range(const key_type& k);
pair<const_iterator,const_iterator>
equal_range(const key_type& k) const;
//find first element with key k
iterator lower_bound(const key_type& k);
const_iterator lower_bound(const key_type& k) const;
//find first element with key greater than k
iterator upper_bound(const key_type& k);
const_iterator upper_bound(const key_type& k) const;
#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef int clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;
int main(int argc,char** argv)
{
typedef multimap<clientId,clientInfo> clientEdp;
typedef multimap<clientId,clientInfo>::const_iterator iterator;
clientEdp clients;
clientInfo client[20];
char* str=new char[10];
string strAddr("socket addr client ");
for(int i=0;i<10;i++)
{
client[i].scanRate=i+1;
//convert int to char*
itoa(i+1,str,10);
//concatenate strAddr and str
client[i].socketAddr=strAddr+str;
clients.insert(
make_pair(10,client[i]));
}
for(int i=10;i<20;i++)
{
client[i].scanRate=i+1;
//convert int to char*
itoa(i+1,str,10);
//concatenate strAddr and str
client[i].socketAddr=strAddr+str;
clients.insert(
make_pair(i+1,client[i]));
}
delete str,strAddr;
//find elements with key 10
iterator lb=clients.lower_bound(10);
iterator ub=clients.upper_bound(10);
for(iterator i=lb;i!=ub;i++)
{
cout<<"clientId:"<<i->first<<endl;
cout<<"scanRate:"<<i->second.scanRate<<endl;
cout<<"socketAddr:"<<i->second.socketAddr<<endl;
cout<<endl;
}
}
//find elements with key 10
pair<iterator,iterator> p=clients.equal_range(10);
for(iterator i=p.first;i!=p.second;i++)
{
cout<<"clientId:"<<i->first<<endl;
cout<<"scanRate:"<<i->second.scanRate<<endl;
cout<<"socketAddr:"<<i->second.socketAddr<<endl;
cout<<endl;
}
鎬葷粨
class A=allocator<pair<const Key,T>>鍙傝冩枃鐚細