小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c加加 set和map

 漢江秋月夜 2012-07-25
#include<map>
#include<iostream>
using namespace std;
int main(){                                 //main()不能返回void,否則會(huì)error: ‘::main’ must return ‘int’
    map<int,string> stu;      //定義map對(duì)象
    //向map中插入數(shù)據(jù)的4種方法
    stu.insert(pair<int,string>(1,"stu_one"));        //插入方法1
    stu.insert(map<int,string>::value_type(2,"stu_two"));     //插入方法2
    stu[3]="stu_three";                 //插入方法3
    stu[7]="stu_seven";
    stu[8]="stu_eight";
    stu[9]="stu_nine";
    //插入方法4,可以判斷插入是否成功
    pair<map<int,string>::iterator,bool> Insert_Pair;
    Insert_Pair=stu.insert(pair<int,string>(1,"stu_one"));
    if(Insert_Pair.second==true)
        cout<<"Insert Successgully"<<endl;
    else
        cout<<"Insert Failure"<<endl;       /*輸出Insert Failure*/
         
    map<int,string>::iterator iter;           //定義map迭代器
         
    //數(shù)據(jù)的查找.使用count或find
    for(int k=0;k<10;k++){
        if(!stu.count(k))           //count返回關(guān)鍵字出現(xiàn)的次數(shù),沒(méi)有出現(xiàn)則返回0
            cout<<"Don't find key=="<<k<<endl;
        else
            cout<<stu[k]<<endl;     //stu[k]是key=k時(shí)對(duì)應(yīng)的value值
    }
    iter=stu.find(1);
    if(iter!=stu.end())
        cout<<"Find,the value is "<<iter->second<<endl;
    else
        cout<<"Don't find key==1"<<endl;
 
    //數(shù)據(jù)的遍歷,2種方法
    for(iter=stu.begin();iter!=stu.end();iter++){       //使用迭代器遍歷
        cout<<iter->first<<"  "<<iter->second<<endl;
    }
    int nSize=stu.size();                   //獲取map集合元素的個(gè)數(shù)
    for(int i=0;i<nSize;i++)             //使用下標(biāo)遍歷.i從0到5.使用下標(biāo)直接訪問(wèn)的是value值
        cout<<stu[i]<<endl;     /*注意這里的下標(biāo)不同于數(shù)組的下標(biāo),這里的下標(biāo)指的就是key值.所以只有當(dāng)i為1,2,3時(shí)才有打印輸出*/
 
 
    //數(shù)據(jù)的刪除與清空
    //用clear()清空,用empty()判斷是否為空
    iter=stu.find(2);       //用迭代器使用erase刪除
    stu.erase(iter);
 
    stu.erase(3);           //使用關(guān)鍵字來(lái)刪除
    //把以下的元素全部清除
    stu.erase(stu.begin(),stu.end());
     
    for(iter=stu.begin();iter!=stu.end();iter++){
        cout<<iter->first<<"  "<<iter->second<<endl;
    }
     
    return 0;
}

原文來(lái)自:博客園(華夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun
分類(lèi): Algorithms
0
0
(請(qǐng)您對(duì)文章做出評(píng)價(jià))
博主前一篇:開(kāi)源軟件許可協(xié)議簡(jiǎn)介
博主后一篇:微軟學(xué)術(shù)搜索新特征暴光

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多