c++map的简单用法

来源:互联网 发布:linux 定时器 编辑:程序博客网 时间:2024/05/21 17:44
#include<iostream>#include<map>#include<algorithm>using namespace std;void main(){map<int, int> temp;temp[12] = 45;temp[1] = 8;temp[34] = 9;temp[2] = 2; //这是最简单暴力的方式,进行map的插入map<int, int>::iterator it;for (it = temp.begin(); it != temp.end(); it++){cout << it->first << "  " << it->second << endl;}//map的输出,map默认按照key值进行排序
it = temp.find(100);//查找某个元素,参量是迭代器if (it!=temp.end()){cout << it->first << "  " << it->second << endl;}else{cout << "error" << endl;}it = temp.find(2);temp.erase(it);//erase要配合find的进行使用,参量是迭代器for (it = temp.begin(); it != temp.end(); it++){cout << it->first << "  " << it->second << endl;}}