当前位置: 首页 > 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] 。请你合并所有重叠的区间…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

【杂谈】-递归进化:人工智能的自我改进与监管挑战

递归进化&#xff1a;人工智能的自我改进与监管挑战 文章目录 递归进化&#xff1a;人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管&#xff1f;3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...

SCAU期末笔记 - 数据分析与数据挖掘题库解析

这门怎么题库答案不全啊日 来简单学一下子来 一、选择题&#xff08;可多选&#xff09; 将原始数据进行集成、变换、维度规约、数值规约是在以下哪个步骤的任务?(C) A. 频繁模式挖掘 B.分类和预测 C.数据预处理 D.数据流挖掘 A. 频繁模式挖掘&#xff1a;专注于发现数据中…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

SpringTask-03.入门案例

一.入门案例 启动类&#xff1a; package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...

Rapidio门铃消息FIFO溢出机制

关于RapidIO门铃消息FIFO的溢出机制及其与中断抖动的关系&#xff0c;以下是深入解析&#xff1a; 门铃FIFO溢出的本质 在RapidIO系统中&#xff0c;门铃消息FIFO是硬件控制器内部的缓冲区&#xff0c;用于临时存储接收到的门铃消息&#xff08;Doorbell Message&#xff09;。…...

蓝桥杯3498 01串的熵

问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798&#xff0c; 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…...