C++之STL(6)之 map 与 multimap 关联容器

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

using namespace std;

int main()
{
	map<int, string> a;
	multimap<int, string> ma;

	// 插入 3种方法                       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 << "map里一共有" << a.size() << "个key-value对数据";
	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;
	}

	ma.insert(multimap<int, string>::value_type(3, "Three"));
	ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
	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.c_str();
		cout << endl;
	}

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

	system("pause");
	return 0;
}

总结:

Multimap允许重复元素,map不允许重复

3种插入方法:

a.insert(map<int, string>::value_type(1, "One"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));
count方法

cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;
#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
	map<int, string> a;
	map<string, int> m_Score;
	multimap<int, string> ma;

	// 插入 3种方法                       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";

	m_Score.insert(make_pair("张飞", 99));
	m_Score.insert(make_pair("刘备", 56));
	m_Score["关羽"] = 87;

	// 对map进行查找
	cout << "最简单的查找:" << endl;
	cout << m_Score["刘备"] << endl;
	cout << a[3] << endl;


	cout << "map里一共有" << a.size() << "个key-value对数据";
	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;
	}

	ma.insert(multimap<int, string>::value_type(3, "Three"));
	ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
	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.c_str();
		cout << endl;
	}

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

	// 查找
	multimap<int, string>::iterator fi;
	// 不能通过迭代器进行修改
	fi =  ma.find(45);
	if (fi != ma.end())
	{
		cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;;
	}
	else
		cout << "没有找到!" << endl;

	// 找重复的
	fi = ma.find(1000);
	if (fi != ma.end())
	{
		cout << "找到了1000!" << endl;
		size_t n = ma.count(1000);
		for (size_t i = 0; i < n; i++)
		{
			cout << "\t Key:" << fi->first;
			cout << ", Value [" << i << "] = ";
			cout << fi->second.c_str() << endl;
			++fi;
		}
	}
	else
	{
		cout << "没找到1000!" << endl;
	}

	system("pause");
	return 0;
}





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

using namespace std;

int main()
{
	map<int, string> a;
	map<string, int> m_Score;
	multimap<int, string> ma;

	// 插入 3种方法                       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";

	m_Score.insert(make_pair("张飞", 99));
	m_Score.insert(make_pair("刘备", 56));
	m_Score["关羽"] = 87;// 这个方法是用的最多的

	// 对map进行查找
	cout << "最简单的查找:" << endl;
	cout << m_Score["刘备"] << endl;
	cout << a[3] << endl;


	cout << "map里一共有" << a.size() << "个key-value对数据";
	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;
	}

	ma.insert(multimap<int, string>::value_type(3, "Three"));
	ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
	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.c_str();
		cout << endl;
	}

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

	// 查找
	multimap<int, string>::iterator fi;
	// 不能通过迭代器进行修改
	fi =  ma.find(45);
	if (fi != ma.end())
	{
		cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;;
	}
	else
		cout << "没有找到!" << endl;

	// 找重复的
	fi = ma.find(1000);
	if (fi != ma.end())
	{
		cout << "找到了1000!" << endl;
		size_t n = ma.count(1000);
		for (size_t i = 0; i < n; i++)
		{
			cout << "\t Key:" << fi->first;
			cout << ", Value [" << i << "] = ";
			cout << fi->second.c_str() << endl;
			++fi;
		}
	}
	else
	{
		cout << "没找到1000!" << endl;
	}

	// 删除方法
	if (ma.erase(-1) > 0)
	{
		cout << "删除成功-1!" << endl;
	}
	// 删除方法
	multimap<int, string>::iterator iEllementFound = ma.find(45);
	if (iEllementFound != ma.end())
	{
		ma.erase(iEllementFound);
		cout << "删除45成功" << endl;
	}
	// 删除方法
	ma.erase(ma.lower_bound(1000), ma.upper_bound(1000));// 删除一个范围



	system("pause");
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值