C++基础语法——unordered_map和unordered_set

news/2024/10/23 9:36:50/

目录

1. unordered系列关联式容器

2.unordered_map

①unordered_map的简介

②unordered_map的构造

③unordered_map的容量

④unordered_map的迭代器

⑤unordered_map的元素访问

⑥unordered_map的查询

⑦unordered_map的修改

⑧unordered_map的桶操作

3.使用与对比测试


1. unordered系列关联式容器

在C++98中,STL提供了底层为红黑树结构的一系列关联式容器,在查询时效率可达到log(N),即最差情况下需要比较红黑树的高度次,当树中的节点非常多时,查询效率也不理想。最好的查询是,进行很少的比较次数就能够将元素找到,因此在C++11中,STL又提供了4个unordered系列的关联式容器,这四个容器与红黑树结构的关联式容器使用方式基本类似,只是其底层结构不同,本文中只对unordered_map和unordered_set进行介绍,unordered_multimap和unordered_multiset则与之前的multimap和multiset相似,这里就不做介绍。

2.unordered_map

①unordered_map的简介

源文档如下

大致翻译如下

1. unordered_map是存储<key, value>键值对的关联式容器,其允许通过keys快速的索引到与其对应的value。
2. 在unordered_map中,键值通常用于惟一地标识元素,而映射值是一个对象,其内容与此键关联。键和映射值的类型可能不同。
3. 在内部,unordered_map没有对<kye, value>按照任何特定的顺序排序, 为了能在常数范围内找到key所对应的value,unordered_map将相同哈希值的键值对放在相同的桶中。
4. unordered_map容器通过key访问单个元素要比map快,但它通常在遍历元素子集的范围迭代方面效率较低。
5. unordered_maps实现了直接访问操作符(operator[]),它允许使用key作为参数直接访问value。
6. 它的迭代器至少是前向迭代器。

②unordered_map的构造

文档如下

③unordered_map的容量

文档如下

④unordered_map的迭代器

文档如下

⑤unordered_map的元素访问

文档如下

⑥unordered_map的查询

文档如下

⑦unordered_map的修改

文档如下

⑧unordered_map的桶操作

文档如下

注: unordered_set与其类似用法将在使用中举出。

3.使用与对比测试

#include<iostream>
#include<unordered_set>
#include<unordered_map>
#include<set>
using namespace std;// 使用测试int main()
{// 去重unordered_set<int> us;us.insert(3);us.insert(1);us.insert(3);us.insert(4);us.insert(5);us.insert(0);unordered_set<int>::iterator it = us.begin();while (it != us.end()){cout << *it << " ";++it;}cout << endl;unordered_map<string, string> dict;dict["sort"] = "排序";dict["insert"] = "插入";dict["string"] = "字符串";dict["left"];for (auto& kv : dict){cout << kv.first << ":" << kv.second << endl;}cout << endl;return 0;
}效率测试
//
//int main()
//{
//	const size_t N = 100000;
//
//	unordered_set<int> us;
//	set<int> s;
//
//	vector<int> v;
//	v.reserve(N);
//	srand(time(0));
//	for (size_t i = 0; i < N; ++i)
//	{
//		v.push_back(rand());
//		//v.push_back(rand()+i);
//		//v.push_back(i);
//	}
//
//	size_t begin1 = clock();
//	for (auto e : v)
//	{
//		s.insert(e);
//	}
//	size_t end1 = clock();
//	cout << "set insert:" << end1 - begin1 << endl;
//
//	size_t begin2 = clock();
//	for (auto e : v)
//	{
//		us.insert(e);
//	}
//	size_t end2 = clock();
//	cout << "unordered_set insert:" << end2 - begin2 << endl;
//
//
//	size_t begin3 = clock();
//	for (auto e : v)
//	{
//		s.find(e);
//	}
//	size_t end3 = clock();
//	cout << "set find:" << end3 - begin3 << endl;
//
//	size_t begin4 = clock();
//	for (auto e : v)
//	{
//		us.find(e);
//	}
//	size_t end4 = clock();
//	cout << "unordered_set find:" << end4 - begin4 << endl << endl;
//
//	cout <<"插入数据个数:"<< s.size() << endl;
//	cout <<"插入数据个数:" << us.size() << endl << endl;;
//
//	size_t begin5 = clock();
//	for (auto e : v)
//	{
//		s.erase(e);
//	}
//	size_t end5 = clock();
//	cout << "set erase:" << end5 - begin5 << endl;
//
//	size_t begin6 = clock();
//	for (auto e : v)
//	{
//		us.erase(e);
//	}
//	size_t end6 = clock();
//	cout << "unordered_set erase:" << end6 - begin6 << endl << endl;
//	
//	return 0;
//}

运行如下


http://www.ppmy.cn/news/1138550.html

相关文章

tkinter中如何执行,单击按钮后的线程操作

在Tkinter中&#xff0c;按钮可以绑定一个回调函数来处理点击事件。如果你想在按钮点击时执行一个线程操作&#xff0c;可以在回调函数中创建一个新的线程来处理这个操作。 #我的Python教程 #官方微信公众号&#xff1a;wdPython以下是一个简单的示例代码&#xff0c;演示如何…

条件查询和数据查询

一、后端 1.controller层 package com.like.controller;import com.like.common.CommonDto; import com.like.entity.User; import com.like.service.UserService; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import …

【C语言】模拟实现strstr

strstr这个库函数看到这个名字大概率猜不到这是什么函数&#xff0c; 但经过学习就可以很好的认识到这个函数 目录 介绍&#xff1a;模拟实现&#xff1a;思路&#xff1a;代码实现&#xff1a; 介绍&#xff1a; 可以看到此函数是用来寻找一个字符串中是否含有另一个字符串 代…

视频号规则改动,不再支持拍单,传统无货源模式已行不通!

视频号小店批量铺货行不通了&#xff0c;大家好我是派大星&#xff0c;这两天视频号发布了一个公告&#xff0c; 核心信息呢就是10月7号&#xff0c;视频号小店&#xff0c;将无法直接查看消费者的详细下单信息&#xff0c;只能通过电子面单的形式&#xff0c;打单发货。每个店…

CTFHUB - SSRF

目录 SSRF漏洞 攻击对象 攻击形式 产生漏洞的函数 file_get_contents() fsockopen() curl_exec() 提高危害 利用的伪协议 file dict gopher 内网访问 伪协议读取文件 端口扫描 POST请求 总结 上传文件 总结 FastCGI协议 CGI和FastCGI的区别 FastCGI协议 …

《protobuf》基础语法3

文章目录 默认值更新规则保留字段未知字段 默认值 在反序列化时&#xff0c;若被反序列化的二进制序列中不包含某个字段&#xff0c;则在反序列化时&#xff0c;就会设置对应默认值。不同的类型默认值不同&#xff1a; 类型默认值字符串“”布尔型false数值类型0枚举型0设置了…

找不到msvcp140.dll是什么意思?三个快速解决msvcp140.dll丢失问题的方法

msvcp140.dll 丢失意味着您的计算机上缺少Microsoft Visual C 2015 Redistributable中的一个动态链接库文件。msvcp140.dll是该软件包中的一个组件&#xff0c;许多应用程序和游戏都需要这个动态链接库文件才能正常运行。当您尝试运行需要 msvcp140.dll 的应用程序或游戏时&…

leetcode做题笔记164. 最大间距

给定一个无序的数组 nums&#xff0c;返回 数组在排序之后&#xff0c;相邻元素之间最大的差值 。如果数组元素个数小于 2&#xff0c;则返回 0 。 您必须编写一个在「线性时间」内运行并使用「线性额外空间」的算法。 示例 1: 输入: nums [3,6,9,1] 输出: 3 解释: 排序后的…