C++map/multimap容器

1.基本概念

map中存放的每一个元素是一个pair对组,第一个元素是key,第二个元素是value。map中的元素不允许重复,但是multimap元素可重复

2.创建

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
	//创建一个map
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	//map中的元素会根据key排序
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout<<it->first<<it->second;
	}
}

3.基本操作

1,大小和交换

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
	
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	if (!m.empty()) {//是否为空
		cout << m.size();//2
	}
	map<int, int> m2;
	m2.insert(pair<int, int>(3, 30));
	m2.insert(pair<int, int>(4, 40));
	m2.swap(m);
	for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
		cout << it->first << "="<<it->second<<endl;//1=10 2=20
	}
	
}

2,插入和删除

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {	
	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;
	//按照key删除
	m.erase(2);
	//清空
	m.clear();
}

3.查找和统计

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {	
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 30));
	map<int,int>::iterator it= m.find(2);
	if (it !=m.end()) {
		cout << "找到元素";
		//map不允许插入重复的元素,结果只有0和1,但是multimap可能大于1
		int num = m.count(2);
		cout << num;
	}
}

5.map容器自定义排序规则

默认是按照key排序,如何自定义排序规则,通过仿函数实现。代码如下

#include <iostream>
#include <map>
#include <string>
using namespace std;
//自定义仿函数
class myCompare {
public:
	bool operator()(int a, int b)const {
		return a > b;//降序排序
	}
};
int main() {	
	map<int, int,myCompare> m;//第三个参数可以传入自己定义的规则
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 30));
	for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << it->first;
		cout << it->second;
	}
}

如果是自定义数据类型,仿函数传入两个自定义数据类型即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++ STL中的mapmultimap是关联容器,用于存储键值对(key-value pairs),其中每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map中出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例中,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map中的所有键值对,并输出每个键值对的键和值。 multimap的用法与map类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序三两行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值