map操作

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值