当前位置: 首页 > news >正文

c++11 标准模板(STL)(std::unordered_set)(二)

定义于头文件 <unordered_set>
template<

    class Key,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator<Key>

> class unordered_set;
(1)(C++11 起)
namespace pmr {

    template <class Key,
              class Hash = std::hash<Key>,
              class Pred = std::equal_to<Key>>
    using unordered_set = std::unordered_set<Key, Hash, Pred,
                                             std::pmr::polymorphic_allocator<Key>>;

}
(2)(C++17 起)

unordered_set is 是含有 Key 类型唯一对象集合的关联容器。搜索、插入和移除拥有平均常数时间复杂度。

在内部,元素并不以任何特别顺序排序,而是组织进桶中。元素被放进哪个桶完全依赖其值的哈希。这允许对单独元素的快速访问,因为哈希一旦,就准确指代元素被放入的桶。

不可修改容器元素(即使通过非 const 迭代器),因为修改可能更改元素的哈希,并破坏容器。

 

成员函数

构造 unordered_set

std::unordered_set<Key,Hash,KeyEqual,Allocator>::unordered_set
unordered_set() : unordered_set( size_type(/*implementation-defined*/) ) {}

explicit unordered_set( size_type bucket_count,
                        const Hash& hash = Hash(),
                        const key_equal& equal = key_equal(),

                        const Allocator& alloc = Allocator() );
(1)(C++11 起)
unordered_set( size_type bucket_count,

               const Allocator& alloc )
              : unordered_set(bucket_count, Hash(), key_equal(), alloc) {}
unordered_set( size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )

              : unordered_set(bucket_count, hash, key_equal(), alloc) {}
(1)(C++14 起)

explicit unordered_set( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count = /*implementation-defined*/,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(2)(C++11 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count,
               const Allocator& alloc )
              : unordered_set(first, last,

                  bucket_count, Hash(), key_equal(), alloc) {}
(2)(C++14 起)
template< class InputIt >

unordered_set( InputIt first, InputIt last,
               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
              : unordered_set(first, last,

                  bucket_count, hash, key_equal(), alloc) {}
(2)(C++14 起)

unordered_set( const unordered_set& other );

(3)(C++11 起)

unordered_set( const unordered_set& other, const Allocator& alloc );

(3)(C++11 起)

unordered_set( unordered_set&& other );

(4)(C++11 起)

unordered_set( unordered_set&& other, const Allocator& alloc );

(4)(C++11 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count = /*implementation-defined*/,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(5)(C++11 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Allocator& alloc )
              : unordered_set(init, bucket_count,

                  Hash(), key_equal(), alloc) {}
(5)(C++14 起)
unordered_set( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
              : unordered_set(init, bucket_count,

                  hash, key_equal(), alloc) {}
(5)(C++14 起)

从各种数据源构造新容器。可选的以用户提供的 bucket_count 为用于创建的最小桶数,以 hash 为哈希函数,以 equal 为比较关键的函数,和以 alloc 为分配器。

1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。

2) 构造拥有范围 [first, last) 的内容的容器。设置 max_load_factor() 为 1.0 。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造拥有 other 内容的容器。若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。

5) 构造拥有 initializer_list init 内容的容器,同 unordered_set(init.begin(), init.end()) 。

参数

alloc-用于此容器所有内存分配器的分配器
bucket_count-初始化时用的最小桶数。若不指定,则使用实现定义的默认值
hash-要用的哈希函数
equal-用于此容器所有关键比较的比较函数
first, last-复制元素来源的范围
other-用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

复杂度

1) 常数

2) 平均情况与 firstlast 间的距离成线性,最坏情况成平方。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) 平均情况与 init 的大小成线性,最坏情况成平方。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

析构 unordered_set

std::unordered_set<Key,Hash,KeyEqual,Allocator>::~unordered_set

~unordered_set();

(C++11 起)

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include <time.h>using namespace std;struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator +(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator *(const Cell &cell){x *= cell.x;y *= cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator >(const Cell &cell) const{if (x == cell.x){return y > cell.y;}else{return x > cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};struct myCompare
{bool operator()(const int &a, const int &b){return a < b;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{os << pCell.first << "-" << pCell.second;return os;
}struct CHash
{size_t operator()(const Cell& cell) const{size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;return thash;}
};struct CEqual
{bool operator()(const Cell &a, const Cell &b) const{return a.x == b.x && a.y == b.y;}
};int main()
{std::cout << std::boolalpha;std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));auto generate = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};//1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。std::unordered_set<Cell, CHash, CEqual> unordered_set1;std::cout << "unordered_set1 is empty " << unordered_set1.empty() << std::endl;std::cout << std::endl;std::vector<Cell> vector1(6);std::generate(vector1.begin(), vector1.end(), generate);std::cout << "vector1:          ";std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//2) 构造拥有范围 [first, last) 的内容的容器。//设置 max_load_factor() 为 1.0 。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的。std::unordered_set<Cell, CHash, CEqual> unordered_set2(vector1.begin(), vector1.end());std::cout << "unordered_set2:   ";std::copy(unordered_set2.begin(), unordered_set2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。std::unordered_set<Cell, CHash, CEqual> unordered_set3(unordered_set2);std::cout << "unordered_set3:   ";std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//4) 移动构造函数。用移动语义构造拥有 other 内容的容器。//若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。std::unordered_set<Cell, CHash, CEqual> unordered_set4(std::move(unordered_set2));std::cout << "unordered_set4:   ";std::copy(unordered_set4.begin(), unordered_set4.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//5) 构造拥有 initializer_list init 内容的容器,同 unordered_set(init.begin(), init.end()) 。std::unordered_set<Cell, CHash, CEqual> unordered_set5{generate(), generate(), generate(), generate(), generate(), generate()};std::cout << "unordered_set5:   ";std::copy(unordered_set5.begin(), unordered_set5.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;return 0;
}

输出

 

相关文章:

c++11 标准模板(STL)(std::unordered_set)(二)

定义于头文件 <unordered_set> template< class Key, class Hash std::hash<Key>, class KeyEqual std::equal_to<Key>, class Allocator std::allocator<Key> > class unordered_set;(1)(C11 起)namespace pmr { templ…...

GEE学习笔记 七十二:【GEE之Python版教程六】命令行简介

这篇开始就要讲解GEE相关的内容&#xff0c;首先聊一下命令行的内容&#xff0c;这个在官方文档中有详细的介绍&#xff0c;这里我简单说一下常用的几个命令&#xff0c;剩余的大家在使用过程中如果又需要可以随时查看相关官方文档的介绍。官方文档地址&#xff1a;https://dev…...

DDD单根 聚合根 实体 值对象

前言2004年Eric Evans 发表Domain-Driven Design –Tackling Complexity in the Heart of Software &#xff08;领域驱动设计&#xff09;&#xff0c;简称Evans DDD。快二十年的时间&#xff0c;领域驱动设计在不断地发展&#xff0c;后微服务时代强调的东西&#xff0c;在国…...

SpringMvc介绍。

目录 1、SpringMvc概述 1、基本介绍 2、工作流程 3、bean加载控制 二、请求 1、请求映射路径 2、请求方式 3、请求参数 4、请求参数&#xff08;传递json数据&#xff09; 5、日期类型参数传递 三、响应 四、REST风格 1、REST简介 2、RESTful入门案例 3、RESTfu…...

华为OD机试 - 最小传递延迟(JS)

最小传递延迟 题目 通讯网络中有N个网络节点 用1 ~ N进行标识 网络通过一个有向无环图进行表示 其中图的边的值,表示节点之间的消息传递延迟 现给定相连节点之间的延时列表times[i]={u,v,w} 其中u表示源节点,v表示目的节点,w表示u和v之间的消息传递延时 请计算给定源节点到…...

学生信息管理系统(通讯录)----------通俗易懂、附源码、C语言实现

绪论&#xff1a; 本篇文章使结构体章节后的习题&#xff0c;如果你对C语言有问题&#xff0c;或者结构体有什么问题不妨看看我之前所写的文章&#xff08;章回体&#xff09;,对于文件管理和内存分配问题我将在后面补上&#xff0c;对于这个学生信息管理系统我用了多种方法和…...

Python抽奖系统

#免费源码见文末公众号# 抽奖系统① def choujiang1():def write():with open(d:\\抽奖系统\\抽奖1.1.pickle,rb) as file:lst1pickle.load(file)namevar1.get()if name not in lst1 and name!录入成功&#xff01; and name!录入失败&#xff01; and name!:lst1.append(name)…...

真实景观渲染技巧【Three.js】

受到一些很棒的 three.js 演示、与 covid 相关的旅行禁令以及可能在 pinterest 上花太多时间看美丽的旅行照片的启发——我开始看看我是否可以使用 three.js 和r3f在浏览器中渲染一个令人信服的风景场景。 推荐&#xff1a;将 NSDT场景编辑器 加入你的3D开发工具链。 在过去一个…...

MySQL知识汇总:MySQL函数CASE WHEN用法详解

Case When的两种简单用法 用法一&#xff1a; CASE seasonWHEN Spring THEN 春天 WHEN Summer THEN 夏天 WHEN autumn THEN 秋天 else 冬天 end 用法二&#xff1a; CASE WHEN season Spring THEN 春天WHEN season Summer THEN 夏天WHEN season autumn THEN 秋天 els…...

Python学习-----模块1.0(模块的简介、定义与使用)

目录 前言&#xff1a; 1.什么是模块 2.模块的分类 &#xff08;1&#xff09;内置模块 &#xff08;2&#xff09;第三方模块 &#xff08;3&#xff09;自定义模块 3.模块的使用 4.自定义模块 5.模块和执行文件的判断 前言&#xff1a; 今天就开始讲Python中的模块篇…...

Linux进程学习【二】

✨个人主页&#xff1a; Yohifo &#x1f389;所属专栏&#xff1a; Linux学习之旅 &#x1f38a;每篇一句&#xff1a; 图片来源 &#x1f383;操作环境&#xff1a; CentOS 7.6 阿里云远程服务器 Perseverance is not a long race; it is many short races one after another…...

我问chatGPT,在JavaScript中构造函数和类的区别

问&#xff1a;构造器函数和面向中的类是同样的东西吗|&#xff1f; 答&#xff1a;构造器函数和面向对象中的类并不是同样的东西&#xff0c;它们之间有些许不同。 在面向对象编程中&#xff0c;类是一种抽象的概念&#xff0c;它描述了一类具有相同属性和行为的对象。类可以…...

软考高级-信息系统管理师之沟通管理(最新版)

项目沟通管理 1、项目沟通管理基础项目沟通管理的重要性项目沟通管理相关理论2、规划沟通管理3、管理沟通4、控制沟通项目沟通管理的技术和工具1、项目沟通管理基础 项目沟通管理的重要性 1、与1T项目成功有关的最重要的四个因素是:主管层的支持、用户参与、有经验的项目经理…...

PyQt5 自定义富文本编辑器

介绍 一款使用PyQt5和网页端框架wangEditor集成的富文本编辑器 代码片段 PyQt5客户端 与网页端建立连接def create_connect(self):self.web_view QWebEngineView()self.bridge JSBridge(self.web_view.page())self.web_view.load(QUrl.fromLocalFile(self.editor_path))w…...

【高可用系统架构设计】SLA服务可用性4个9是什么意思?如何保证服务的高可用性 HA(High Availability)?...

如何保证服务的高可用性 HA&#xff08;High Availability&#xff09;?高可用HA&#xff08;High Availability&#xff09;是分布式系统架构设计中必须考虑的因素之一&#xff0c;它通常是指&#xff0c;通过设计减少系统不能提供服务的时间。方法论上&#xff0c;高可用是通…...

微服务架构设计模式-(14)面向生产环境

生产环境要求 应用安全 数据权限 服务可配置性 不同环境的配置不一样&#xff0c;不能写死代码&#xff0c;所以要配置 可观测性 需要日志系统 应用安全 四个方面 身份验证 验证主体的身份解决方案 单体 cookie 微服务中 API Gateway 访问令牌 不透明令牌透明令牌&#xff…...

HTML5新增用法

新增语义化标签 并无特殊含义&#xff0c;是语义&#xff01;语义&#xff01;语义&#xff01; <header> 头部区域 <nav> 导航区域 <main> 主体区域 <article> 内部标签 <section> 块级标签 <aside> 侧边栏标签 <footer> 尾部…...

富足金字塔:人的努力是为了扩大选择的范围

人的努力是为了扩大选择的范围&#xff0c;这是熵减的另一种表述。富足金字塔代表着人生的三重境界。第一层是温饱。人需要食物、水、住所。第二层是品质。能源、ICT、教育带来更有品质的生活&#xff0c;如智能门锁、智能马桶、扫地机、洗碗机、洗衣烘衣机。第三层是梦想。包括…...

C++类基础(十七)

类的继承——补充知识 ● public 与 private 继承&#xff08;C Public, Protected and Private Inheritance&#xff09; 改变了类所继承的成员的访问权限 //公有继承 struct Base { public:int x; private:int y; protected:int z; }; struct Derive : public Base //公有继承…...

LeetCode刷题复盘笔记—一文搞懂贪心算法之56. 合并区间(贪心算法系列第十四篇)

今日主要总结一下可以使用贪心算法解决的一道题目&#xff0c;56. 合并区间 题目&#xff1a;56. 合并区间 Leetcode题目地址 题目描述&#xff1a; 以数组 intervals 表示若干个区间的集合&#xff0c;其中单个区间为 intervals[i] [starti, endi] 。请你合并所有重叠的区间…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

【kafka】Golang实现分布式Masscan任务调度系统

要求&#xff1a; 输出两个程序&#xff0c;一个命令行程序&#xff08;命令行参数用flag&#xff09;和一个服务端程序。 命令行程序支持通过命令行参数配置下发IP或IP段、端口、扫描带宽&#xff0c;然后将消息推送到kafka里面。 服务端程序&#xff1a; 从kafka消费者接收…...

C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。

1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj&#xff0c;再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...

代理篇12|深入理解 Vite中的Proxy接口代理配置

在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...

LLMs 系列实操科普(1)

写在前面&#xff1a; 本期内容我们继续 Andrej Karpathy 的《How I use LLMs》讲座内容&#xff0c;原视频时长 ~130 分钟&#xff0c;以实操演示主流的一些 LLMs 的使用&#xff0c;由于涉及到实操&#xff0c;实际上并不适合以文字整理&#xff0c;但还是决定尽量整理一份笔…...

【网络安全】开源系统getshell漏洞挖掘

审计过程&#xff1a; 在入口文件admin/index.php中&#xff1a; 用户可以通过m,c,a等参数控制加载的文件和方法&#xff0c;在app/system/entrance.php中存在重点代码&#xff1a; 当M_TYPE system并且M_MODULE include时&#xff0c;会设置常量PATH_OWN_FILE为PATH_APP.M_T…...

基于PHP的连锁酒店管理系统

有需要请加文章底部Q哦 可远程调试 基于PHP的连锁酒店管理系统 一 介绍 连锁酒店管理系统基于原生PHP开发&#xff0c;数据库mysql&#xff0c;前端bootstrap。系统角色分为用户和管理员。 技术栈 phpmysqlbootstrapphpstudyvscode 二 功能 用户 1 注册/登录/注销 2 个人中…...

Bean 作用域有哪些?如何答出技术深度?

导语&#xff1a; Spring 面试绕不开 Bean 的作用域问题&#xff0c;这是面试官考察候选人对 Spring 框架理解深度的常见方式。本文将围绕“Spring 中的 Bean 作用域”展开&#xff0c;结合典型面试题及实战场景&#xff0c;帮你厘清重点&#xff0c;打破模板式回答&#xff0c…...

恶补电源:1.电桥

一、元器件的选择 搜索并选择电桥&#xff0c;再multisim中选择FWB&#xff0c;就有各种型号的电桥: 电桥是用来干嘛的呢&#xff1f; 它是一个由四个二极管搭成的“桥梁”形状的电路&#xff0c;用来把交流电&#xff08;AC&#xff09;变成直流电&#xff08;DC&#xff09;。…...

Android写一个捕获全局异常的工具类

项目开发和实际运行过程中难免会遇到异常发生&#xff0c;系统提供了一个可以捕获全局异常的工具Uncaughtexceptionhandler&#xff0c;它是Thread的子类&#xff08;就是package java.lang;里线程的Thread&#xff09;。本文将利用它将设备信息、报错信息以及错误的发生时间都…...