在线编译网站

http://www.compileonline.com/compile_cpp_online.php

#include <iostream>
#include<map>
using namespace std;

int main()
{
//创建map-----map插入的键和值不能完全重复
map<int,string> mp;
map<int,int> mp1;
//插入map----第一种
int value;
string str;
cin>>value;
cin>>str;
mp.insert(pair<int, string>(value, str));
//插入map----第二种
value++;
mp.insert(map<int, string>::value_type (value, str));
//插入map----第三种数组类型插入
value++;
mp[value]=str;
//计算map的大小
int mysize=mp.size();
//遍历map---以输出的形式表示
//第一种遍历map
map<int, string>::iterator iter;
for(iter = mp.begin(); iter != mp.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
//第二种遍历map----利用反向迭代器
return 0;
}

猜你喜欢

转载自www.cnblogs.com/Aiahtwo/p/10764494.html