锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品美女久久久,亚洲午夜久久久影院,久久久久九九精品影院http://www.shnenglu.com/09212744/category/14073.htmlzh-cnSat, 19 Jun 2010 17:06:34 GMTSat, 19 Jun 2010 17:06:34 GMT60C++ Internals: STL涔婱aphttp://www.shnenglu.com/09212744/archive/2010/06/19/118235.html鍚寸Е錛圫aylor錛?/dc:creator>鍚寸Е錛圫aylor錛?/author>Sat, 19 Jun 2010 04:25:00 GMThttp://www.shnenglu.com/09212744/archive/2010/06/19/118235.htmlhttp://www.shnenglu.com/09212744/comments/118235.htmlhttp://www.shnenglu.com/09212744/archive/2010/06/19/118235.html#Feedback1http://www.shnenglu.com/09212744/comments/commentRss/118235.htmlhttp://www.shnenglu.com/09212744/services/trackbacks/118235.html姒傝堪

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錛屼富瑕佸唴瀹瑰涓嬶細

  • 1銆佷緥瀛愬紩鍏?
  • 2銆乵ap涓殑綾誨瀷瀹氫箟
  • 3銆乵ap涓殑榪唬鍣ㄥ拰閿煎
  • 4銆乵ap涓殑鏋勯犲嚱鏁頒笌鏋愭瀯鍑芥暟
  • 5銆乵ap涓殑鎿嶄綔鏂規硶
  • 6銆佸啀璁甿ap鐨勬彃鍏ユ搷浣?
  • 7銆乕]涓嶄粎鎻掑叆
  • 8銆乵ultimap
  • 9銆佹葷粨

1銆佷緥瀛愬紩鍏?/strong>

鏈変竴涓湇鍔″櫒manager緇存姢鐫鎺ュ叆鏈嶅姟鍣ㄧ殑client淇℃伅錛屽寘鎷琧linetId銆乻canRate銆乻ocketAddr絳夌瓑銆傛垜浠畾涔変竴涓粨鏋勪綋淇濆瓨scanRate銆乻ocketAddr淇℃伅銆傚涓嬶細

typedef    int    clientId;
typedef struct{
int scanRate;
string socketAddr;
}clientInfo;

鎴戜滑鐢╩ap淇濆瓨榪欎簺淇℃伅錛歝lientId涓洪敭key錛宑lientInfo涓哄箋傝繖鏍鋒垜浠彲浠ラ氳繃clientId蹇熸绱㈠埌client鐨勭浉鍏充俊鎭紝鎴戜滑鍙互榪欐牱瀹氫箟錛?/p>

map<clientId,clientInfo> clientMap;

榪欐牱鎴戜滑瀹氫箟浜嗕竴涓猚lientMap錛屽鏋滄垜浠瀹氫箟澶氫釜榪欐牱鐨刴ap錛岄渶瑕佸嬈″啓map<clientId,clientInfo> 鍙橀噺鍚嶃備負浜嗛伩鍏嶈繖鏍鋒儏鍐碉紝鎴戜滑閫氬父涓簃ap<clientId,clientInfo>瀹氫箟涓埆鍚嶏紝濡傦細

typedef map<clientId,clientInfo> clientEdp;
clientEdp clientMap;

涔嬪悗鎴戜滑灝卞彲浠ュ儚瀹氫箟clientMap涓鏍峰畾涔塵ap<clientId,clientInfo>瀵硅薄錛岃繖鏍風殑濂藉榪樻湁錛氬鏋滄垜浠渶瑕佷慨鏀筸ap鐨勫畾涔夛紝鍙渶瑕佸湪涓澶勪慨鏀瑰嵆鍙紝閬垮厤淇敼涓嶅交搴曢犳垚鐨勪笉涓鑷寸幇璞°?/p>

鎴戜滑榪欏氨瀹屾垚浜嗛渶瑕佺殑map鐨勫畾涔夛紝濡傛灉涓嶅畾涔夋垨娌℃湁鍦ㄥ畠涓婇潰鐨勬搷浣滅殑璇濓紝灝卞儚瀹氫箟綾昏屾病鏈夋柟娉曚竴鏍鳳紝鎰忎箟涓嶅ぇ鎴栨鏃犳剰涔夈傚垢榪愮殑鏄紝STL鎻愪緵浜嗚繖浜涘父鐢ㄦ搷浣滐細鎺掑簭錛堟敞錛歮ap鏄笉鑳戒篃涓嶈鎺掑簭鐨勶紝鍥犱負map鏈韓宸茬粡鎺掑ソ搴忎簡錛夈佹墦鍗般佹彁鍙栧瓙閮ㄥ垎銆佺Щ闄ゅ厓绱犮佹坊鍔犲厓绱犮佹煡鎵懼璞★紝灝卞儚鏁版嵁搴撶殑澧炲垹鏀規煡鎿嶄綔錛佺幇鍦ㄦ垜浠緇嗕粙緇嶈繖浜涙搷浣滐紝騫墮愭寮曞叆hash_map銆?em>multimap銆?/p>

2銆乵ap涓殑綾誨瀷瀹氫箟

鍏寵仈鏁扮粍錛?em>associative array錛夋槸鏈鏈夌敤鐨勭敤鎴峰畾涔夌被鍨嬩箣涓錛岀粡甯稿唴緗湪璇█涓敤浜庢枃鏈鐞嗙瓑銆備竴涓叧鑱旀暟緇勯氬父涔熺О涓簃ap錛屾湁鏃朵篃縐板瓧鍏革紙dictionary錛夛紝淇濆瓨涓瀵瑰箋傜涓涓肩О涓簁ey銆佺浜屼釜縐頒負鏄犲皠鍊糾apped-value銆?/p>

鏍囧噯map鏄畾涔夊湪std鍛藉悕絀洪棿涓殑涓涓ā鏉匡紝騫惰〃紺轟負<map>銆傚畠棣栧厛瀹氫箟浜嗕竴緇勬爣鍑嗙被鍨嬪悕瀛楋細

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;
//...
}

娉ㄦ剰錛歮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>

map<int,clientInfo> clientMap;

Cmp鍜孉閮界己鐪併?閫氬父錛屽疄闄呯殑榪唬鍣ㄦ槸瀹炵幇瀹氫箟鐨勶紝鍥犱負map寰堝儚浣跨敤浜嗘爲鐨勫艦寮忥紝榪欎簺榪唬鍣ㄩ氬父鎻愪緵鏍戦亶鍘嗙殑鏌愮褰㈠紡銆傞嗗悜榪唬鍣ㄦ槸浣跨敤鏍囧噯鐨剅everse_iterator妯℃澘鏋勯犵殑銆?/p>

3銆乵ap涓殑榪唬鍣ㄥ拰閿煎

map鎻愪緵鎯父鐨勮繑鍥炶凱浠e櫒鐨勪竴緇勫嚱鏁幫紝濡備笅鎵紺猴細

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;
//...
}

map涓婄殑榪唬鍣ㄦ槸pair<const Key,mapped_type>鍏冪礌搴忓垪涓婄畝鍗曠殑榪唬銆備緥濡傦紝鎴戜滑鍙兘闇瑕佹墦鍗板嚭鎵鏈夌殑瀹㈡埛绔俊鎭紝鍍忎笅闈㈢殑紼嬪簭榪欐牱銆備負浜嗗疄鐜拌繖涓紝鎴戜滑棣栧厛鍚戙婁緥瀛愬紩鍏ャ嬩腑瀹氫箟鐨刢lientEdp涓彃鍏ユ暟鎹紝鐒跺悗鎵撳嵃鍑烘潵錛?/p>

#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;
}
}

涓涓猰ap榪唬鍣ㄤ互key鍗囧簭鏂瑰紡琛ㄧず鍏冪礌錛屽洜姝ゅ鎴風淇℃伅浠liendId鍗囧簭鐨勬柟寮忚緭鍑恒傝繍琛岀粨鏋滃彲浠ヨ瘉鏄庤繖涓鐐癸紝榪愯緇撴灉濡備笅鎵紺猴細

image

鍥?銆佺▼搴忚繍琛岀粨鏋?/p>

鎴戜滑浠irst寮曠敤閿煎鐨刱ey錛屼互second寮曠敤mapped value錛屼笖涓嶇敤綆ey鍜宮apped value鏄粈涔堢被鍨嬨傚叾瀹瀙air鍦╯td鐨勬ā鏉夸腑鏄繖鏍峰畾涔夌殑錛?/p>

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){}
}

鍗砿ap涓紝key鏄敭鍊煎鐨勭涓涓厓绱犱笖mapped value鏄浜屼釜鍏冪礌銆俻air鐨勫畾涔夊彲浠ュ湪<utility>涓壘鍒幫紝pair鎻愪緵浜嗕竴涓柟娉曟柟渚垮垱寤洪敭鍊煎錛?/p>

template <class T1,class T2>pair<T1,T2>
std::make_pair(const T1& t1,const T2& t2)
{
return pair<T1,T2>(t1,t2);
}

涓婇潰鐨勪緥瀛愪腑鎴戜滑灝辯敤鍒頒簡榪欎釜鏂規硶鏉ュ垱寤?clientId,clientInfo)瀵癸紝騫朵綔涓篒nsert()鏂規硶鐨勫弬鏁般傛瘡涓猵air榛樿鍒濆鍖栨瘡涓厓绱犵殑鍊間負瀵瑰簲綾誨瀷鐨勯粯璁ゅ箋?/p>

4銆乵ap涓殑鏋勯犲嚱鏁頒笌鏋愭瀯鍑芥暟

map綾繪儻甯告彁渚涗簡鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟錛屽涓嬫墍紺猴細

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&);
//...
}

澶嶅埗涓涓鍣ㄦ剰鍛崇潃涓哄畠鐨勬瘡涓厓绱犲垎閰嶇┖闂達紝騫舵嫹璐濇瘡涓厓绱犲箋傝繖鏍峰仛鏄ц兘寮閿鏄緢澶х殑錛屽簲璇ヤ粎褰撻渶瑕佺殑鏃跺欐墠榪欐牱鍋氥?strong>鍥犳錛宮ap浼犵殑鏄紩鐢?/strong>銆?/p>

5銆乵ap涓殑鎿嶄綔鏂規硶

鍓嶉潰鎴戜滑宸茬粡璇磋繃錛屽鏋渕ap涓粎瀹氫箟浜嗕竴浜沰ey銆乵apped value綾誨瀷鐨勪俊鎭屾病鏈夋搷浣滄柟娉曪紝灝卞瀹氫箟涓粎鏈夊瓧孌電殑綾繪剰涔変笉澶х敋鑷蟲鏃犳剰涔夈傜敱姝ゅ彲瑙乵ap涓畾涔夋搷浣滄柟娉曢潪甯擱噸瑕侊紒鍓嶉潰鐨勪緥瀛愭垜浠氨鐢ㄥ埌浜嗕笉灝戞柟娉曪紝濡傝繑鍥炶凱浠e櫒鐨勬柟娉昩egin()銆乪nd()錛岄敭鍊煎鎻掑叆鏂規硶insert()銆備笅闈㈡垜浠map涓殑鎿嶄綔鏂規硶鍋氫釜鍏ㄩ潰鐨勪粙緇嶏細

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&);
//...
}

涓婇潰榪欎簺鏂規硶鍩烘湰閮借兘欏懼悕鎬濅箟錛圥S.鐢辨鍙錛屽懡鍚嶆湁澶氶噸瑕侊紝鎴戜滑騫蟲椂瑕佸吇鎴愬ソ鐨勫懡鍚嶄範鎯紝褰撶劧娉ㄩ噴涔熷繀涓嶅彲灝戯紒錛夈傝櫧鐒跺凡緇忛潪甯告竻妤氫簡浜嗭紝浣嗘垜榪樻槸鎯寵瑙d竴涓嬩互娑堥櫎涓嶆儨瑕佺殑璇В鍜屾洿濂藉湴搴旂敤榪欎簺鏂規硶銆?/p>

  • find(k)鏂規硶綆鍗曞湴榪斿洖閿間負k鐨勫厓绱犵殑榪唬鍣紱濡傛灉娌℃湁鍏冪礌鐨勯敭鍊間負k錛屽垯榪斿洖map鐨別nd()榪唬鍣ㄣ傜敱浜巑ap鏄寜閿甼ey鍗囧簭鎺掑垪錛屾墍鏈夋煡鎵劇殑澶嶆潅搴﹀彧鏈塐(logN)銆傚洜姝わ紝鎴戜滑閫氬父浼氳繖鏍風敤榪欎釜鏂規硶錛?
    #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;
        }
        }
  • insert()鏂規硶 璇曞浘灝嗕竴涓紙Key,T錛夐敭鍊煎鍔犲叆map銆傚洜涓洪敭鏃跺敮涓鐨勶紝鎵浠ヤ粎褰搈ap涓笉瀛樺湪閿間負k鐨勯敭鍊煎鏃舵彃鍏ユ墠鎴愬姛銆傝鏂規硶鐨勮繑鍥炲間負pair<iterator,bool>錛屽鏋滄彃鍏ユ垚鍔焍ool鍊間負TRUE錛宨terator鎸囧悜鎻掑叆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;
        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;
        }

涓婇潰鎴戜滑鐪嬪嚭錛岃繖閲屾垜浠彃鍏ラ敭鍊煎鏄鍏堝0鏄庝竴涓敭鍊煎pair<clientId,clientInfo> p110(id,cltInfo); 鐒跺悗鍐嶆彃鍏ワ紝榪欎釜鎴戜滑涔嬪墠make_pair鏂規硶涓嶄竴鏍鳳紝make_pair鏂規硶鐢ㄧ殑姣旇緝澶氥?/p>

  • erase()鏂規硶鐢ㄦ硶姣旇緝綆鍗曪紝姣斿鍍忔竻闄lientId涓?10鐨勯敭鍊煎錛屾垜浠彧闇瑕佸clients璋冪敤erase鏂規硶錛?span style="color: #ff8040;">clients.erase(clients.find(110));鎴栬呮垜浠兂娓呴櫎clientId浠?鍒?0鐨勯敭鍊煎錛屾垜浠彲浠ヨ繖鏍瘋皟鐢╡rase()鏂規硶錛?span style="color: #ff8040;">clients.erase(clients.finds(1),clients.find(10));綆鍗曞惂錛佸埆寰楁剰錛屼綘榪橀渶瑕佹敞鎰忥紝濡傛灉find(k)榪斿洖鐨勬槸end()錛岃繖鏍瘋皟鐢╡rase()鏂規硶鍒欐槸涓涓弗閲嶇殑閿欒錛屼細瀵筸ap閫犳垚鐮村潖鎿嶄綔銆?

6銆佸啀璁甿ap鐨勬彃鍏ユ搷浣?/strong>

鍓嶉潰鎴戜滑浠嬬粛浜嗗埄鐢╩ap鐨勬彃鍏ユ柟娉昳nsert()錛屽0鏄庨敭鍊煎pair鎴杕ake_pair鐢熸垚閿煎鐒跺悗鎴戜滑鍙互杞繪澗鐨勫皢閿煎鎻掑叆map涓傚叾瀹瀖ap榪樻彁渚涗簡鏇存柟渚跨殑鎻掑叆鎿嶄綔鍒╃敤涓嬫爣錛坰ubscripting錛孾]錛夋搷浣滐紝濡備笅錛?/p>

clientInfo cltInfo;
cltInfo.scanRate=10;
cltInfo.socketAddr="110";
clients[110]=cltInfo;

榪欐牱鎴戜滑灝卞彲浠ョ畝鍗曞湴灝嗛敭鍊煎鎻掑叆鍒癿ap涓簡銆備笅鏍囨搷浣滃湪map涓紡榪欐牱瀹氫箟鐨勶細

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);
//...
}

鎴戜滑鏉ュ垎鏋愪竴涓嬪簲鐢╗]鎿嶄綔錛屾彃鍏ラ敭鍊煎鐨勮繃紼嬶細媯鏌ラ敭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>

//////////////////////////////////////////////
///@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));
}
}

Note2錛?/strong>鎴戜滑瑙嗕箮榪樺拷鐣ヤ簡涓鐐癸紝濡傛灉鏄犲皠鍊糾apped value鐨勭被鍨嬫病鏈夐粯璁ゅ鹼紝鎬庝箞鍔烇紵榪欑鎯呭喌璇峰嬁浣跨敤[]鎿嶄綔鎻掑叆銆?/p>

7銆乕]涓嶄粎鎻掑叆

閫氳繃[]鎿嶄綔涓嶄粎浠呮槸鎻掑叆閿煎錛屾垜浠篃鍙互閫氳繃閿甼ey媯绱㈠嚭鏄犲皠鍊糾apped value銆傝屼笖鎴戜滑鍒╃敤[]鎿嶄綔鍙互杞繪澗鍦扮粺璁′俊鎭紝濡傛湁榪欐牱榪欐牱涓浜涢敭鍊煎錛坆ook-name錛宑ount錛夊錛?/p>

(book1,1)銆?book2,2)銆?book1,2)銆?book3,1)銆?book3,5)

鎴戜滑璁$畻姣忕book鐨勬暟閲忔誨拰銆傛垜浠彲浠ヨ繖鏍峰仛錛氬皢瀹冧滑璇誨叆涓涓猰ap<string,int>錛?/p>

#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;
}

緇撴灉濡備笅鎵紺猴細錛堟敞鎰忔寜浣廲trl+z閿粨鏉熻緭鍏ワ級

image

鍥?銆佺▼搴忚繍琛岀粨鏋?/p>

8銆乵ultimap

鍓嶉潰浠嬬粛浜唌ap錛屽彲浠ヨ宸茬粡闈炲父娓呮櫚浜嗐傚鏋滃厑璁竎lientId閲嶅鐨勮瘽錛宮ap灝辨棤鑳戒負鍔涗簡錛岃繖鏃跺欏氨寰梞ultimap涓婂満浜嗭紒multimap鍏佽閿甼ey閲嶅錛屽嵆涓涓敭瀵瑰簲澶氫釜鏄犲皠鍊箋?/strong>鍏跺疄闄ゆ涔嬪錛宮ultimap璺焟ap鏄緢鍍忕殑錛屾垜浠帴涓嬫潵鍦╩ap鐨勫熀紜涓婁粙緇峬ultimap銆?/p>

multimap鍦╯td涓殑瀹氫箟璺焟ap涓鏍峰彧鏄被鍚嶄負multimap錛宮ultimap鍑犱箮鏈塵ap鐨勬墍鏈夋柟娉曞拰綾誨瀷瀹氫箟銆?/p>

  • multimap涓嶆敮鎸乕]鎿嶄綔錛涗絾map鏀寔
  • multimap鐨刬nsert鏂規硶榪斿洖鐨勬槸涓涓凱浠e櫒iterator錛屾病鏈塨ool鍊鹼紱鑰宮ap鍊鹼紙iterator錛宐ool錛夌殑鍏冪礌瀵?
  • 瀵瑰簲equal_range()銆佹柟娉曪細
    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;
    铏界劧鍦╩ap鍜宮ultimap閮芥湁錛屾樉鐒跺multimap鏈夋洿澶氱殑鎰忎箟錛乪qual_range()鏂規硶榪斿洖涓涓敭key瀵瑰簲鐨勫涓槧灝勫肩殑涓婄晫鍜屼笅鐣岀殑閿煎鐨勮凱浠e櫒銆乴ower_bound()鏂規硶榪斿洖閿甿ultimap涓涓涓涓簁ey鐨勯敭鍊煎榪唬鍣ㄣ乽pper_bound()鏂規硶榪斿洖姣攌ey澶х殑絎竴涓敭鍊煎榪唬鍣ㄣ?

鍋囪鎴戜滑鎯沖彇鍑洪敭涓簁ey鐨勬墍鏈夋槧灝勫鹼紝鎴戜滑鍙互榪欐牱鍋氾細

#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;
}
}

錛堣鏄庯細瀹為檯涓婏紝涓鑸槸涓嶅厑璁竎lientId閲嶅鐨勶紝榪欓噷鍙槸涓轟簡涓句緥銆傦級榪欐牱鏄笉鏄劅瑙夊緢涓戝憿錛佷簨瀹炰笂錛屾垜浠彲浠ユ洿綆鍗曠殑榪欐牱錛?/p>

//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;
}

鎬葷粨

map鏄竴綾誨叧鑱斿紡瀹瑰櫒銆傚畠鐨勭壒鐐規槸澧炲姞鍜屽垹闄よ妭鐐瑰榪唬鍣ㄧ殑褰卞搷寰堝皬錛岄櫎浜嗛偅涓搷浣滆妭鐐癸紝瀵瑰叾浠栫殑鑺傜偣閮芥病鏈変粈涔堝獎鍝嶃傚浜庤凱浠e櫒鏉ヨ錛屽彲浠ヤ慨鏀瑰疄鍊鹼紝鑰屼笉鑳戒慨鏀筴ey銆?

map鐨勫姛鑳斤細

  • 鑷姩寤虹珛Key 錛峷alue鐨勫搴斻俴ey 鍜寁alue鍙互鏄換鎰忎綘闇瑕佺殑綾誨瀷銆?
  • 鏍規嵁key鍊煎揩閫熸煡鎵捐褰曪紝鏌ユ壘鐨勫鏉傚害鍩烘湰鏄疞og(N)銆?
  • 蹇熸彃鍏ey - Value 璁板綍銆?
  • 蹇熷垹闄よ褰?
  • 鏍規嵁Key 淇敼value璁板綍銆?
  • 閬嶅巻鎵鏈夎褰曘?

灞曟湜錛氭湰鏂囦笉鐭ヤ笉瑙夊啓浜嗕笉灝戝瓧浜嗭紝浣嗕粛鏈繁鍏ユ秹鍙婂埌map瀹氫箟鐨勭3涓拰絎?涓弬鏁幫紝浣跨敤鐨勯兘鏄粯璁ゅ箋?/p>

template<class Key,class T,class Cmp=less<key>,
    class A=allocator<pair<const Key,T>>

鎰熷叴瓚h咃紝璇鋒煡鎵劇浉鍏寵祫鏂檕r涓嬮潰鐣欒█甯屾湜鐪嬪埌鍗曠嫭寮綃囦粙緇峬ap絎?涓拰絎?涓弬鏁般傛偍鐨勬敮鎸侊紝鎴戠殑鍔ㄥ姏錛丳S錛氬湪姝ゆ枃鐨勫師鍥狅紝鍦ㄤ笌鍏徃鍋氶」鐩敤鍒頒簡map鐗規鎬葷粨鍑烘潵涓庡ぇ瀹跺叡浜紝涓嶈繃鍦ㄨ繘琛屼釜浜烘葷粨榪囩▼涓紝闅懼厤浼氭湁鐤忔紡鎴栦笉褰撲箣澶勶紝璇蜂笉鍚濇寚鍑恒?/p>

鍙傝冩枃鐚細

銆?銆戙奣he C++ Programming Language (Special Edition)銆?/p>

銆?銆戙奅ffective STL銆?/p>

]]>
无码国产69精品久久久久网站| 国产精品久久一区二区三区| 午夜精品久久影院蜜桃| 人人妻久久人人澡人人爽人人精品| 久久亚洲AV无码精品色午夜麻豆| 久久综合给合久久狠狠狠97色69 | 要久久爱在线免费观看| 狠狠色丁香久久婷婷综合_中| 丁香色欲久久久久久综合网| 9191精品国产免费久久| 天堂久久天堂AV色综合| 国产成人精品久久亚洲高清不卡 | 欧美亚洲国产精品久久高清| 国产成人精品久久一区二区三区av | 久久精品视频91| 久久久久久亚洲AV无码专区| 亚洲人成网站999久久久综合 | 2021国产精品久久精品| 国产精品日韩深夜福利久久| 久久久久AV综合网成人| 伊人久久大香线蕉综合Av | 久久久久久亚洲精品影院| 国内精品久久九九国产精品| 中文字幕日本人妻久久久免费| 久久久中文字幕日本| 99久久精品无码一区二区毛片| 97r久久精品国产99国产精| 亚洲国产精品久久电影欧美| 久久久久无码国产精品不卡| 亚洲狠狠综合久久| 99久久www免费人成精品| 精品久久久久久| 亚洲国产精品人久久| 亚洲国产天堂久久综合网站 | 久久夜色精品国产欧美乱| 精品多毛少妇人妻AV免费久久 | 久久久久久av无码免费看大片| 国産精品久久久久久久| 久久中文字幕视频、最近更新| 亚洲精品无码久久久久AV麻豆| 亚洲欧洲中文日韩久久AV乱码|