STL之Map容器

一、map容器的基本概念

简介:

      map中所有元素都是pair

      pair中的第一个元素为key(键值),起到索引作用,第二个元素为value(实值)

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

本质:map/multimap属于关联式容器,底层结构是二叉树实现。

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

map与multimap的区别:

      map不允许容器中有重复的key值元素。

      multimap允许容器中有重复的key值元素。

二、map构造和赋值

学习目标:对map容器进行构造和赋值操作

构造函数原型:

      map<T1,T2> mp;                 //默认构造

      map(const map &mp);        //拷贝构造

赋值函数原型:

      map& operator=(const  map &mp);           //重载等号操作符

示例:

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

using namespace std;
void PrintMap01(const map<int, int> &m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key=" << (*it).first << " Value=" << it->second << endl;
	}
	cout << endl;
}
void MapTest39()
{
	//两个数据类型,一个代表key值,一个代表value值
	map<int, int> m;
	//m.insert(2, 10);  //非法的间接寻址	
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(3, 30));
	PrintMap01(m);

	//2、拷贝构造
	cout << "拷贝构造:" << endl;
	map<int, int> m2(m);
	PrintMap01(m2);

	//3、重载等号操作符
	cout << "重载等号操作符:" << endl;
	map<int, int> m3;
	m3 = m;
	PrintMap01(m3);
}
int main()
{
	MapTest39();
	system("pause");
	return 0;
}

总结:map中的所有元素都是成对出现的,插入数据时要使用对组

三、map容器的大小与交换

学习目标:统计map容器的大小与交换map容器

函数原型:

      size();               //返回容器中元素个数

      empty();          

     swap(st);          //交换两个集合容器

示例: 

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

using namespace std;
void PrintMap02(const map<int, string> &m)
{
	for (map<int, string>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key=" << it->first << " Value=" << (*it).second << endl;
	}
	cout << endl;
}
void MapTest40()
{
	map<int, string> m;
	//1、插入
	m.insert(pair<int, string>(1, "老一"));
	m.insert(pair<int, string>(2, "老二"));
	m.insert(pair<int, string>(3, "老三"));
	m.insert(pair<int, string>(4 ,"老四"));
	m.insert(pair<int, string>(5, "老五"));
	PrintMap02(m);
	//2、大小
	if (!m.empty())
	{
		cout << "m容器的大小为:" << m.size() << endl;
	}
	else
	{
		cout << "m容器为空!" << endl;
	}

	//3、交换
	map<int, string> m1;
	//1、插入
	m1.insert(pair<int, string>(1, "科一"));
	m1.insert(pair<int, string>(2, "科二"));
	m1.insert(pair<int, string>(3, "科三"));

	cout << "交换前:" << endl;
	PrintMap02(m);
	PrintMap02(m1);
	cout << "交换后:" << endl;
	m1.swap(m);
	PrintMap02(m);
	PrintMap02(m1);
}
int main()
{
	MapTest40();
	system("pause");
	return 0;
}

四、map容器的插入与删除

学习目标:对map容器进行数据的插入与删除

函数原型:

      insert(elem);           //在容器中插入元素

      clear();                    //清除所有元素

      erase(pos);             //删除pos迭代器所指向的元素,返回下一个元素的迭代器

      erase(beg,end)       //删除[beg,end)区间的迭代器所指向的元素,返回下一个元素的迭代器

      erase(key);             //删除容器中值为key的元素

 示例:

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

using namespace std;
void PrintMap03(const map<int, int> &m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key=" << it->first << " Value=" << (*it).second << endl;
	}
	cout << endl;
}
void MapTest41()
{
	map<int, int> m;
	//1、插入
	//第一种插入
	m.insert(pair<int, int>(1, 10));
	//第二种插入方式
	m.insert(make_pair(2, 20));
	//第三种插入方式
	m.insert(map<int, int>::value_type(3, 30));
	//第四种方式
	m[4] = 40;            //不建议使用,但可以用key来访问到value
	cout << m[5] << endl;
	PrintMap03(m);

	//2、删除erase()
	m.erase(m.begin());
	PrintMap03(m);

	m.erase(3);
	PrintMap03(m);

	m.erase(m.begin(), m.end());
	PrintMap03(m);
	m.insert(make_pair(5, 50));
	PrintMap03(m);

	cout << "清除操作:" << endl;
	m.clear();
	PrintMap03(m);

}
int main()
{
	MapTest41();
	system("pause");
	return 0;
}

五、map容器的查找与统计

学习目标:对map容器进行数据查找与统计

函数原型:

      find(key);           //查找key元素是否存在,若存在,则返回迭代器,若不存在,返回set.end();

      count(key);       //统计key的元素个数

 示例:

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

using namespace std;

void MapTest42()
{
	map<int,int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	//查找
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "查找到了元素: Key=" << pos->first << " Value=" << (*pos).second << endl;
	}
	else
	{
		cout << "没有查找到元素" << endl;
	}
	//统计      map容器中不允许插入重复的key值  对于count统计要么是0,要么是1
	int num = m.count(3);
	cout << "Num=" << num << endl;
}
int main()
{
	MapTest42();
	system("pause");
	return 0;
}

六、map容器的排序

学习目标:利用仿函数,改变排序规则

map容器的默认排序规则:按照key值进行,从小到大排序

示例:

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

using namespace std;
class MapPerson {
public:
	MapPerson() {}
	MapPerson(string name, int age)
	{
		this->M_name = name;
		this->M_age = age;
	}
	string M_name;
	int M_age;
};
class MapCompare {
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
	bool operator()(MapPerson p1, MapPerson p2)   //不能传入地址或引用
	{
		return p1.M_age < p2.M_age;
	}
};
class MapComparePER {
public:
	bool operator()(MapPerson p1, MapPerson p2)
	{
		return p1.M_age > p2.M_age;
	}
};

void MapTest43()
{
	map<int, int, MapCompare> m;       //加仿函数后,后面的排序规则会根据仿函数的规则进行排序
	m.insert(make_pair(5, 50));
	//m[5] = 50;           //这种ing不属于(int,int)表达式,比较时会报错错
	m.insert(pair<int, int>(1, 10));
	m.insert(map<int, int>::value_type(3, 30));
	m.insert(make_pair(2, 10));
	m.insert(pair<int, int>(4, 40));

	cout << "map容器的默认排序:" << endl;
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key=" << it->first << " Value=" << (*it).second << endl;
	}

	//改变map容器的默认排序规则
	cout << "改变map容器的默认排序规则" << endl;

	for (map<int, int, MapCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "Key=" << it->first << " Value=" << (*it).second << endl;
	}

	map<MapPerson,int, MapCompare > mp;
	MapPerson p1("小黄", 22);
	MapPerson p3("小红", 2);
	MapPerson p2("小新", 12);
	MapPerson p4("小东", 32);
	mp.insert(pair<MapPerson, int>(p1, 1));
	mp.insert(pair<MapPerson, int>(p4, 4));
	mp.insert(pair<MapPerson, int>(p3, 3));
	mp.insert(pair<MapPerson, int>(p2, 2));
	cout << "自定义数据类型没有默认排序规则,必须指定排序规则,否则报错。" << endl;
	for (map<MapPerson, int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << "Key=" << (*it).first.M_name << "+" << (*it).first.M_age << " Value=" <<it->second<<endl;
	}

	cout << "自定义数据类型自定义排序后:" << endl;
	for (map<MapPerson, int, MapCompare >::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << "Key=" << (*it).first.M_name << "+" << (*it).first.M_age << " Value=" << it->second << endl;
	}
}
int main()
{
	MapTest43();
	system("pause");
	return 0;
}

总结:对于内置的数据类型,map容器有自己的默认排序规则。但对于自定义数据类型,map容器必须指定排序规则,否则,会报错

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)提供了一个名为`std::map`的关联容器,它基于红黑树实现,用于存储键值对,并按照键的顺序进行排序。下面是`std::map`容器的一些常用方法: 1. 插入元素: ```cpp std::map<Key, Value> myMap; myMap.insert(std::make_pair(key, value)); // 使用insert方法插入键值对 myMap[key] = value; // 使用下标操作符[]插入键值对,如果键已存在,则会更新值 ``` 2. 删除元素: ```cpp myMap.erase(key); // 根据键删除元素 myMap.clear(); // 清空所有元素 ``` 3. 访问元素: ```cpp Value value = myMap[key]; // 使用下标操作符[]访问指定键对应的值 auto it = myMap.find(key); // 使用find方法查找指定键的迭代器 if (it != myMap.end()) { Value value = it->second; // 通过迭代器访问指定键对应的值 } ``` 4. 遍历容器: ```cpp for (const auto& pair : myMap) { Key key = pair.first; // 键 Value value = pair.second; // 值 // 其他操作 } ``` 5. 判断元素是否存在: ```cpp if (myMap.count(key) > 0) { // 键存在 } ``` 6. 获取容器大小和判断容器是否为空: ```cpp size_t size = myMap.size(); // 获取容器中键值对的个数 bool isEmpty = myMap.empty(); // 判断容器是否为空 ``` 这些是`std::map`容器的一些常用方法,还有其他一些方法和成员函数可以进一步扩展其功能。你可以参考C++标准库的文档以获取更详细的信息。 希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值