栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ STL进阶与补充(map/multimap容器)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ STL进阶与补充(map/multimap容器)

Map中所有的容器都是pair。对组中每个中都有两个值,第一个值称为key值(键值),起到索引作用,第二个元素为value(实值)。

所有元素会根据元素的键值自动排序。

Map/multimap属于关联式容器,底层架构是用二叉树实现。

优点:可以根据key值快速找到value值。

Map和multimap的区别:与set和multiset一样,主要区分key值有没有重复,value不做区分。

(本节关于对组容器的介绍请参考本人其他博客:《C++ STL进阶与补充(set/multiset容器)》)

1、map构造和赋值

Map mp;

默认构造

Map(const map& mp);

拷贝构造

=

等号赋值

#include
#include
using namespace std;
void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
	}
}
void test01()
{
	map m1;  //默认构造
	m1.insert(pair(1, 10));  //每次都插入匿名对组
	m1.insert(pair(2, 20));
	m1.insert(pair(5, 50));
	m1.insert(pair(3, 30));
	m1.insert(pair(4, 40));
	printMap(m1);  //会自动排序

	//拷贝构造
	map m2(m1);
	printMap(m2);
	//赋值
	map m3;
	m3 = m2;
	printMap(m3);
}
int main()
{
	test01();
}

2、map大小和交换

Size()

返回容器中元素个数

Empty()

判断容器是否为空

Swap(st)

交换两个集合容器

#include
#include
using namespace std;
void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
	}
}
void test01()
{
	map m1;  
	m1.insert(pair(1, 10));  
	m1.insert(pair(2, 20));
	m1.insert(pair(5, 50));
	if (m1.empty())
	{
		cout << "m为空!" << endl;
	}
	else
	{
		cout << "m不为空,m的大小为:" << m1.size() << endl;
	}
	printMap(m1);  //会自动排序
}
void test02()
{
	map m1;
	m1.insert(pair(1, 10));
	m1.insert(pair(2, 20));
	m1.insert(pair(3, 30));
	map m2;
	m2.insert(pair(4, 100));
	m2.insert(pair(5, 200));
	m2.insert(pair(6, 300));
	cout << "交换前:" << endl;
	printMap(m1);
	printMap(m2);
	cout << "交换后:" << endl;
	m2.swap(m1);
	printMap(m1);
	printMap(m2);
}
int main()
{
	//test01();
	test02();
}

3、map插入和删除

Insert(elem)

插入元素,每个elem都是对组

Clear()

清空

Erase(pos)

删除pos迭代器位置的元素,返回下一个元素的迭代器

Erase(beg,end)

删除区间[beg,end]的所有元素,返回下一个元素的迭代器

Erase(key)

删除容器中键值为key的数据

#include
#include
using namespace std;
void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
	}
}
void test01()
{
	map m1;  
	//第一种插入
	m1.insert(pair(1, 10));  
	//第二种插入
	m1.insert(make_pair(2,20));
	//第三种插入
	m1.insert(map::value_type(3, 30));
	//第四种插入:中括号方式,但是不建议
	//中括号的用途主要用于根据键值访问实值
	m1[4] = 40;
	cout << m1[5] << endl; //虽然没有5,但是会自动创建5
	printMap(m1);
	//按照迭代器位置删除
	m1.erase(m1.begin());
	printMap(m1);
	//按照key删除
	m1.erase(5);
	printMap(m1);
}
int main()
{
	test01();
}

4、map查找和统计

对map容器进行查找数据及统计数据。

Find(key)

查找key是否存在,若存在则返回键的元素的迭代器;若不存在,返回map.end()

Count(key)

统计key的元素个数,对于map容器,只有0和1两种结果。

#include
#include
using namespace std;
void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
	}
}
void test01()
{
	map m1;  
	m1.insert(pair(1, 10));  
	m1.insert(pair(2, 20));
	m1.insert(pair(3, 30));
	map::iterator pos = m1.find(3);
	if (pos != m1.end())
	{
		cout << "找到元素key=" << (*pos).first<< endl;
		cout << "找到元素value=" << (*pos).second<< endl;
	}
	else
	{
		cout << "未找到该元素!" << endl;
	}
	//统计
	int num = m1.count(3);
	cout << "num=" << num << endl;
}
int main()
{
	test01();
}

5、map排序

Map容器默认的排序规则为按照key值从小到大排序,可利用仿函数改变排序规则。

对于自定义数据类型,必须要指定排序规则。

#include
#include
using namespace std;
class myCompare
{
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};
void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key: " << (*it).first << " Value: " << (*it).second << endl;
	}
}
void test01()
{
	map m1;  
	m1.insert(pair(1, 10));  
	m1.insert(pair(2, 20));
	m1.insert(pair(3, 30));
	m1.insert(pair(4, 40));
	printMap(m1);
}
int main()
{
	test01();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993906.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号