unordered_map(哈希表)与map(红黑树)容器的方法记录

本文详细记录了C++标准库中的unordered_map(哈希表)和map(红黑树)两种容器的主要方法和使用场景。通过比较它们的插入、查找、删除等操作的时间复杂度,探讨了在不同需求下如何选择合适的容器。

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

//unordered_map的方法记录
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;

int main()
{
	
	unordered_map<int,string> test;//声明 
	test.insert(make_pair(1,"hello"));//插入元素
	test.insert(make_pair(13,"hello13"));
	test.insert(make_pair(14,"hello14"));
	test.insert(make_pair(15,"hello15"));
	
	//pair形式的insert,可以有pair的返回值,first是相应map的迭代器指针,指向这个元素,second是bool,插入成功与否 
	pair<unordered_map<int,string>::iterator,bool> return_pair;

	return_pair = test.insert(make_pair(2,"hello2"));
	cout<<"insert data="<<return_pair.first->second<<endl; 
	cout<<"insert2="<<return_pair.second<<endl;

	//再次插入相同键值的时候,不能插入成功,不做操作 
	return_pair = test.insert(make_pair(2,"lalala"));
	cout<<"insert2 again="<<return_pair.second<<endl;
	cout<<"insert data="<<return_pair.first->second<<endl; 

	//也可以以数组方式插入
	test[3] = "hello3";
	
	unordered_map<int,string>::iterator it;//迭代器
	//迭代器查找元素 
	it = test.find(3);
	if(it!=test.end())
		cout<<"find success"<<it->second<<endl;
	else
		cout<<"find failed"<<endl; 
	 
	if(test.empty())	cout<<"empty"<<endl;//检查是否为空 
	else cout<<"not empty"<<endl; 
	
	//删除指定元素
	it = test.find(3);//迭代器找到再删除 
	test.erase(it);
	int n = test.erase(2);//删除了返回1,没删除成功返回0
	cout<<"erase="<<n<<endl;
	n = test.erase(2);
	cout<<"erase="<<n<<endl;
	
	test.insert(make_pair(19,"hello19"));
	test.insert(make_pair(20,"hello20"));
	test.insert(make_pair(56,"hello56"));
	test.insert(make_pair(19,"hello19"));
	//元素遍历
	for(it = test.begin();it!=test.end();it++) cout<<"key="<<it->first<<" value="<<it->second<<endl;
	

	
	//元素全部删除
	test.clear();
	test.erase(test.begin(),test.end());

	return 0;
} 

//map(红黑树)方法记录
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;

int main()
{
	map<int,string> test_map;//声明 
	test_map.insert(make_pair(1,"hello"));//插入元素
	test_map.insert(make_pair(13,"hello13"));
	test_map.insert(make_pair(14,"hello14"));
	test_map.insert(make_pair(15,"hello15"));
	
	//pair形式的insert,可以有pair的返回值,first是相应map的迭代器指针,指向这个元素,second是bool,插入成功与否 
	pair<map<int,string>::iterator,bool> return_pair_map;

	return_pair_map = test_map.insert(make_pair(2,"hello2"));
	cout<<"insert data="<<return_pair_map.first->second<<endl; 
	cout<<"insert2="<<return_pair_map.second<<endl;

	//再次插入相同键值的时候,不能插入成功,不做操作 
	return_pair_map = test_map.insert(make_pair(2,"lalala"));
	cout<<"insert2 again="<<return_pair_map.second<<endl;
	cout<<"insert data="<<return_pair_map.first->second<<endl; 

	//也可以以数组方式插入
	test_map[3] = "hello3";
	
	map<int,string>::iterator it;//迭代器
	//迭代器查找元素 
	it = test_map.find(3);
	if(it!=test_map.end())
		cout<<"find success"<<it->second<<endl;
	else
		cout<<"find failed"<<endl; 
	 
	if(test_map.empty())	cout<<"empty"<<endl;//检查是否为空 
	else cout<<"not empty"<<endl; 
	
	//删除指定元素
	it = test_map.find(3);//迭代器找到再删除 
	test_map.erase(it);
	int n = test_map.erase(2);//删除了返回1,没删除成功返回0
	cout<<"erase="<<n<<endl;
	n = test_map.erase(2);
	cout<<"erase="<<n<<endl;
	
	test_map.insert(make_pair(10,"hello10"));
	test_map.insert(make_pair(90,"hello90"));
	test_map.insert(make_pair(50,"hello50"));
	//元素遍历
	for(it = test_map.begin();it!=test_map.end();it++) cout<<"key="<<it->first<<" value="<<it->second<<endl;
	

	//反向迭代器
	map<int,string>::reverse_iterator rit;
	for(rit = test_map.rbegin();rit!=test_map.rend();rit++) cout<<"key="<<rit->first<<" value="<<rit->second<<endl;
	
	
	//元素全部删除
	test_map.clear();
	test_map.erase(test_map.begin(),test_map.end());
	
	

	
	


	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值