map映射

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
	map<int, string> a; // map是映射,
	multimap<int, string> ma;  // multimap是多映射,

	//                                 key键  value值
	a.insert(map<int, string>::value_type(1, "one"));
	a.insert(map<int, string>::value_type(2, "two"));
	a.insert(map<int, string>::value_type(3, "three"));
	a.insert(make_pair(-1, "minus one"));
	a.insert(pair<int, string>(1000, "one thousand"));
	a[1000000] = "one million";

	cout << "最简单的查找。" << endl;
	cout << a[3] << endl; // 输出的是three,
	

	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();
		cout << endl;
	}

	if (a.erase(-1) > 0)
		cout << endl << "删除-1成功!" << endl;

	map<int, string>::iterator iElementFound = a.find(1000);
	if (iElementFound != a.end())
	{
		a.erase(iElementFound);
		cout << "输出1000成功," << endl;
	}

	a.erase(a.lower_bound(1), a.upper_bound(1));  // 删除所有的1,从第一个到最后一个,

	/*ma.insert(multimap<int, string>::value_type(3, "three"));
	ma.insert(multimap<int, string>::value_type(45, "Forty five")); // multimap允许找重复的数据,
	ma.insert(make_pair(-1, "minus one"));
	ma.insert(pair<int, string>(1000, "one thousand"));
	ma.insert(pair<int, string>(1000, "one thousand"));

	cout << endl << "multimap里有" << 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;
		cout << endl;
	}

	cout << "multimap里有" << ma.count(1000) << "个1000!" << endl;*/

    
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值