c++中的dictionary对象:map的使用备忘
#include <map>
#include<string> //一定要加,和string.h 不一样 #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();
}
可嵌套使用:
map <string, pair<int, int>> resources; resources.insert(pair<string, pair<int, int>>("name",pair<int, int>(5,7)));
怎么读取呢?
string data = resources[string("ABC.TTX")].first;
桂棹兮兰桨,击空明兮溯流光。