日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

c加加 set和map

 漢江秋月夜 2012-07-25
#include<map>
#include<iostream>
using namespace std;
int main(){                                 //main()不能返回void,否則會error: ‘::main’ must return ‘int’
    map<int,string> stu;      //定義map對象
    //向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ù),沒有出現(xiàn)則返回0
            cout<<"Don't find key=="<<k<<endl;
        else
            cout<<stu[k]<<endl;     //stu[k]是key=k時對應的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集合元素的個數(shù)
    for(int i=0;i<nSize;i++)             //使用下標遍歷.i從0到5.使用下標直接訪問的是value值
        cout<<stu[i]<<endl;     /*注意這里的下標不同于數(shù)組的下標,這里的下標指的就是key值.所以只有當i為1,2,3時才有打印輸出*/
 
 
    //數(shù)據(jù)的刪除與清空
    //用clear()清空,用empty()判斷是否為空
    iter=stu.find(2);       //用迭代器使用erase刪除
    stu.erase(iter);
 
    stu.erase(3);           //使用關(guān)鍵字來刪除
    //把以下的元素全部清除
    stu.erase(stu.begin(),stu.end());
     
    for(iter=stu.begin();iter!=stu.end();iter++){
        cout<<iter->first<<"  "<<iter->second<<endl;
    }
     
    return 0;
}

原文來自:博客園(華夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun
分類: Algorithms
0
0
(請您對文章做出評價)
博主前一篇:開源軟件許可協(xié)議簡介
博主后一篇:微軟學術(shù)搜索新特征暴光

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多