#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
int main()
{
//容器保存大量的数据
map<int, string> a;
multimap<int, string> ma;//栈可重复
//map可以把数据快速的一对一对的放进去
//插入数据有四种方法
a.insert(map<int, string>::value_type(1, "one"));//1
a.insert(map<int, string>::value_type(2, "two"));//1
a.insert(map<int, string>::value_type(3, "twree"));//1
a.insert(make_pair(-1, "minus one"));//2
a.insert(pair<int, string>(1000, "onr thoused"));//3
a[100000] = "one million";//4。不能用在muitiple
cout << "map一共有" << a.size() << "个key—value对" << endl;
cout << "这些数据是" << endl;
map<int, string>::const_iterator i;//常迭代器
for (i = a.begin(); i != a.end(); ++i)
{
cout << "KEY:" << i->first;
cout << "value:" << i->second.c_str() << endl;//second是一个string,变成c语言的字符串显示出来
}
ma.insert(multimap<int, string>::value_type(3, "threre"));
ma.insert(multimap<int, string>::value_type(45, "fortyfive"));
ma.insert(make_pair(-1, "minus one"));
ma.insert(pair<int, string>(1000, "one throussand"));
ma.insert(pair<int, string>(1000, "one thousssand"));
cout << endl << "multiple里有" << ma.size() << "个数据" << endl;
multimap<int, string>::const_iterator im;
for (im = ma.begin(); im != ma.end(); ++im)
{
cout << "key:" << im->first;
cout << "value:" << im->second.c_str();
cout << endl;
}
cout << "multiple里有" << ma.count(1000) << "个1000" << endl;
//find查找
multimap<int, string>::const_iterator fi;//常迭代器
fi=ma.find(45);//查找的结果是一个迭代器 ,所以先声明一个迭代器
if (fi != ma.end())
{
cout << "ok" << endl;
}
else{
cout << "error" << endl;
}
cout << a[3] << endl;
//ma。erase()删除操作,删除成功大于0
if (ma.erase(-1) > 0)//删除操作1
{
cout << "删除OK" << endl;
}
multimap<int, string>::iterator iElementFound = ma.find(45);//删除操作不能是const2
if (iElementFound != ma.end())
{
ma.erase(iElementFound);
cout << "删除" << endl;
}
//删除掉所有的1000
ma.erase(ma.lower_bound(1000),ma.upper_bound(1000));//返回的是一个迭代器2
map<string, int> BBB;
BBB.insert(make_pair("张飞",99));
BBB.insert(make_pair("刘备", 56));
BBB["关羽"] = 66;
system("pause");
return 0;
}
map操作
最新推荐文章于 2024-08-18 21:16:27 发布