LeetCode刷题小结之map、unordered_map的使用

本文介绍了LeetCode刷题过程中map和unordered_map的特点与应用场景。map以红黑树实现,保证键的唯一性和有序性,适合对顺序有要求的问题;unordered_map通过哈希表提供更快的查找速度,但键值无序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map

1.特点:map存储key->value键值对,在一个map中仅存在唯一的key。可以实现快速插入、查找、遍历。内部由红黑树实现,所以查找速度上慢于哈希实现的unordered_map,但是元素按照key顺序排列,所以在对顺序有要求的问题中可以使用map。

2.常用方法:

#include <map>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
	map<int, string> test;
	test.insert(pair<int, string>(1, "a")); //插入元素,使用pair声明一个键值对
	test.insert(map<int, string>::value_type(4, "b")); //插入元素,使用value_type方法声明一个键值对
	test[3] = "c"; //插入元素,使用数组方法
				   //使用insert方法时,若插入的key已存在,则插入失败;使用数组方法,key已存在时覆盖

	cout << test[4] << endl; //取元素
	for (map<int, string>::iterator it = test.begin(); it != test.end(); ++it)
		cout << it->first << " " << it->second << endl;  //使用迭代器遍历
	
	map<int, string>::iterator it = test.find(3); //查找key为3的值,若存在,返回对应迭代器;反之,返回test.end()

	test.erase(it); //按照迭代器删除元素
	test.erase(1);  //按照key值删除元素

}
3.自定义数据结构的map

#include <map>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
	int id;
	string name;
	Student (int _id, string _name):id(_id), name(_name){}
	//Student ():id(0), name(""){}

	bool operator  <(const Student& s) const{ //重载小于号运算符
		return id < s.id;
	}
};

int main()
{
	map<Student, int> test;
	test.insert(pair<Student, int>(Student(123, "xiaoming"), 2));
	test.insert(pair<Student, int>(Student(242, "xiaoli"), 3));
	
	cout << test.begin()->second; //输出为2
}
参考: C++ map

Unordered_map

1.unordered_map与map的不同在于他的在key值无序,内部由哈希实现,根据哈希值判断元素是否相同,所以拥有更快的查找速度。

2.常用方法除内部无序外,与map类似。

3.自定义数据结构使用:

#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
struct Student {
	int id;
	string name;
	Student (int _id, string _name):id(_id), name(_name){}
	//Student ():id(0), name(""){}

	bool operator  <(const Student& s) const{ //重载小于号运算符
		return id < s.id;
	}
};
class MyEqualTo {  //自定义等号
public:
	bool operator()(const Student& a, const Student& b)const {
		return a.id == b.id;
	}
};
class MyHash {  //自定义hash
public:
	size_t operator()(const Student& a)const {
		hash<int> sh;        //使用STL中hash<int>
		return sh(a.id);
	}
};
int main()
{
	unordered_map<Student, int, MyHash, MyEqualTo> test;
	test.insert(pair<Student, int>(Student(123, "xiaoming"), 2));
	test.insert(pair<Student, int>(Student(242, "xiaoli"), 3));
	
	cout << test.begin()->second; //输出为2
}
参考:C++ unordered_map


unordered_map中,second是一个成员变量,它存储着unordered_map中每个元素的值。具体来说,当我们使用键来访问unordered_map中的值时,我们可以通过使用键值对的.first成员来获取键,通过使用.second成员来获取对应的值。例如,对于一个unordered_map<int, string>,我们可以使用以下方式访问second的值: ```cpp unordered_map<int, string> myMap; myMap = "one"; myMap = "two"; cout << myMap<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mapunordered_map使用和差别?unordered_map查询效率这么高,为啥还需要map?](https://blog.youkuaiyun.com/JMW1407/article/details/108169804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【C++】unordered_set 和 unordered_map 使用 | 封装](https://blog.youkuaiyun.com/qq_62939852/article/details/130896774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [LeetCode判断字符串是否循环-myLeetcode:leetcode日志](https://download.youkuaiyun.com/download/weixin_38550834/19950070)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值