#include<iostream> #include<map> #include<algorithm> #include<string> using namespace std; void fun(map<int,int>::reference i) { cout<<i.first<<' '<<i.second<<endl; } int main() { map<int,string>student;//第一种方式创建 map<int,int>num;//第二种方式创建 student[1]="Alice";//用[] 插入元素 student[2]="LiHua"; student.insert(map<int,string>::value_type(3,"Lily"));//用insert插入 num[1]=1; num[2]=2; num.insert(map<int,int>::value_type(3,3)); for(map<int,string>::iterator i=student.begin();i!=student.end();i++)//用迭代器遍历 cout<<i->first<<' '<<i->second<<endl; for_each(num.begin(),num.end(),fun);//用for_each遍历 map<int,string>::iterator j=student.find(1);//查找元素 cout<<j->first<<' '<<j->second<<endl; student.erase(student.begin());//删除元素 return 0; }