c++中的dictionary對象:map的使用備忘

#include <map>

#include <iostream>

using namespace std;

void main()

{

? ? map <string, int> dic;

? ? dic.insert(pair<string, int>("a", 5));

? ? dic.insert(pair<string, int>("b", 2));

? ? dic.insert(pair<string, int>("a", 3)); //allow duplicate key

? ? dic.insert(pair<string, int>("c", 7));

? ? dic.insert(pair<string, int>("d", 9));

? ? dic.insert(pair<string, int>("e", 20));

? ? dic["c"] = 8;

? ? map<string,int>::iterator finder;;

? ? finder=dic.find("c");

? ? if(finder==dic.end())

? ? ? ? cout<<"we do not find 112"<<endl;

? ? else

? ? {

? ? ? ? cout<<"total:"<<dic.size()<<endl;

? ? ? ? cout<<"wo find c:"<<dic["c"]<<endl;

? ? ? ? dic.erase(finder); //delete

? ? ? ? cout<<"total:"<<dic.size()<<endl;

? ? }

? ? int c =0;

? ? c = getchar();

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容