当前位置: 首页 > 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 迭代器),因为修改可能更改元素的哈希,并破坏容器。

成员函数

赋值给容器

std::unordered_set<Key,Hash,KeyEqual,Allocator>::operator=

unordered_set& operator=( const unordered_set& other );

(1)(C++11 起)

unordered_set& operator=( unordered_set&& other );

(2)(C++11 起)
(C++17 前)

unordered_set& operator=( unordered_set&& other ) noexcept(/* see below */);

(C++17 起)

unordered_set& operator=( std::initializer_list<value_type> ilist );

(3)(C++11 起)

替换容器内容。

1) 复制赋值运算符。以 other 的副本替换内容。若 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value 为 true ,则以源分配器的副本替换目标分配器。若源分配器与目标分配器不比较相等,则用目标( *this )分配器销毁内存,然后在复制元素前用 other 的分配器分配。 (C++11 起).、

2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。之后 other 在合法但未指定的状态。若 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value 为 true ,则用源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能取走源内存的所有权,而必须单独移动赋值逐个元素,用自己的分配器按需分配额外的内存。任何情况下,原先在 *this 中的元素要么被销毁,要么以逐元素移动赋值替换。

3) 以 initializer_list ilist 所标识者替换内容。

参数

other-用作数据源的另一容器
ilist-用作数据源的 initializer_list

返回值

*this

复杂度

1) 与 *thisother 的大小成线性。

2) 与 *this 的大小成线性,除非分配器不比较相等且不传播,该情况下与 *thisother 的大小成线性。

3) 与 *thisilist 的大小成线性。

异常

2)noexcept 规定:  noexcept(std::allocator_traits<Allocator>::is_always_equal::value

&& std::is_nothrow_move_assignable<Hash>::value

&& std::is_nothrow_move_assignable<Pred>::value)
(C++17 起)

注意

容器移动赋值(重载 (2) )后,除非不兼容的分配器强制逐元素赋值,否则指向 other 的引用、指针和迭代器(除了尾迭代器)都保持合法,不过指代的元素现在在 *this 中。当前标准通过 §23.2.1[container.requirements.general]/12 中的总括陈述保证这点,而 LWG 2321 下正在考虑更直接的保证。

 

返回相关的分配器

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

allocator_type get_allocator() const;

(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;};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_set1(vector1.begin(), vector1.end());std::cout << "unordered_set1:   ";std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//1) 复制赋值运算符。以 other 的副本替换内容。std::unordered_set<Cell, CHash, CEqual> unordered_set2 = unordered_set1;std::cout << "unordered_set2:   ";std::copy(unordered_set2.begin(), unordered_set2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。//之后 other 在合法但未指定的状态。std::unordered_set<Cell, CHash, CEqual> unordered_set3 = std::move(unordered_set1);std::cout << "unordered_set3:   ";std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//3) 以 initializer_list ilist 所标识者替换内容。std::unordered_set<Cell, CHash, CEqual> unordered_set4 ={generate(), generate(), generate(), generate(), generate(), generate()};std::cout << "unordered_set4:   ";std::copy(unordered_set4.begin(), unordered_set4.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;return 0;
}

输出 

http://www.lryc.cn/news/13599.html

相关文章:

  • 事件循环机制eventLoop?Js事件流?JavaScript如何实现异步编程?
  • 视频播放器倍速、清晰度切换、m3u8下载
  • 将Nginx 核心知识点扒了个底朝天(五)
  • 【基础算法】差分
  • 【LeetCode】剑指 Offer(5)
  • 外包出来,朋友内推我去一家公司,问的实在是太...
  • 刷题记录:牛客NC54585小魂和他的数列 [线段树卡常,真恶心]
  • 2019蓝桥杯真题旋转 C语言/C++
  • <JVM上篇:内存与垃圾回收篇>11 - 垃圾回收相关算法
  • 狂飙Linux平台,软件部署大全
  • 积分球原理及积分球类型介绍
  • Vision Transformer(ViT) 2: 应用及代码讲解
  • 高频面试题|JVM虚拟机的体系结构是什么样的?
  • MyBatis-Plus详细讲解(整合spring Boot)
  • 骨传导耳机是不是智商税?骨传导耳机真的不伤耳吗?
  • 模拟实现string
  • 自监督表征预训练之掩码图像建模
  • 华为OD机试题 - 磁盘容量(JavaScript)| 代码+思路+重要知识点
  • ChatGPT:“抢走你工作的不会是 AI ,而是先掌握 AI 能力的人”
  • 数据结构与算法(Java版) | 线性结构和非线性结构
  • 电商数据查询平台:母婴行业妈妈用品全网热销,头部品牌格局初现
  • STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据
  • 华为OD机试题 - 交换字符(JavaScript)| 代码+思路+重要知识点
  • 最好的工程师像投资者一样思考,而不是建设者
  • Mysql里的ibtmp1文件太大,导致磁盘空间被占满
  • android kotlin 协程(四) 协程间的通信
  • 苹果手机通讯录突然没了怎么恢复?
  • BI知识全解,值得收藏
  • 【机器学习】GBDT
  • C#开发的OpenRA游戏高性能内存访问的方法