当前位置: 首页 > 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>::begin(size_type), 
std::unordered_set<Key,Hash,KeyEqual,Allocator>::cbegin(size_type)

local_iterator begin( size_type n );

(C++11 起)

const_local_iterator begin( size_type n ) const;

(C++11 起)

const_local_iterator cbegin( size_type n ) const;

(C++11 起)

返回指向下标为 n 的桶首元素的迭代器。

参数

n-要访问的桶的下标

返回值

指向首元素的迭代器。

复杂度

常数。

 

返回一个迭代器,指向指定的桶的末尾

std::unordered_set<Key,Hash,KeyEqual,Allocator>::end(size_type), 
std::unordered_set<Key,Hash,KeyEqual,Allocator>::cend(size_type)

local_iterator end( size_type n );

(C++11 起)

const_local_iterator end( size_type n ) const;

(C++11 起)

const_local_iterator cend( size_type n ) const;

(C++11 起)

返回后随下标为 n 的桶的最后元素的元素的迭代器。此元素表现为占位符,试图访问它会导致未定义行为。

参数

n-要访问的桶的下标

返回值

指向后随最后元素的元素的迭代器。

复杂度

常数

 

调用示例

#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 + 110;Cell cell{n, n};return cell;};std::unordered_set<Cell, CHash, CEqual> unordered_set1;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的while (unordered_set1.size() < 6){unordered_set1.insert({generate()});}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;for (std::unordered_set<Cell, CHash, CEqual>::const_iterator cit =unordered_set1.cbegin(); cit != unordered_set1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_set1.bucket(*cit);std::cout << "unordered_set1 bucket(" << *cit << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_set<Cell, CHash, CEqual>::const_local_iterator clit = unordered_set1.begin(bucket);std::cout << "const_local_iterator begin(" << bucket << "):    " << *clit << std::endl;}std::cout << std::endl;for (std::unordered_set<Cell, CHash, CEqual>::const_iterator cit =unordered_set1.cbegin(); cit != unordered_set1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_set1.bucket(*cit);std::cout << "unordered_set1 bucket(" << *cit << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_set<Cell, CHash, CEqual>::local_iterator clit = unordered_set1.begin(bucket);std::cout << "local_iterator begin(" << bucket << "):    " << *clit << std::endl;}std::cout << std::endl;for (std::unordered_set<Cell, CHash, CEqual>::const_iterator cit =unordered_set1.cbegin(); cit != unordered_set1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_set1.bucket(*cit);std::cout << "unordered_set1 bucket(" << *cit << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_set<Cell, CHash, CEqual>::const_local_iterator clit = unordered_set1.cbegin(bucket);std::cout << "const_local_iterator cbegin(" << bucket << "):    " << *clit << std::endl;}return 0;
}

输出

 

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

相关文章:

  • AI/CV大厂笔试LeetCode高频考题之基础核心知识点
  • 华为OD机试题,用 Java 解【静态扫描最优成本】问题
  • 常见无线技术方案介绍
  • 收获满满的2022年
  • react的生命周期
  • scanpy 单细胞分析API接口使用案例
  • 【Vue3 第二十一章】Teleport组件传送
  • 在 Windows Subsystem for Linux (WSL2) 的 Ubuntu 系统上配置 Vulkan 开发环境
  • 放苹果HJ61
  • Windows下,OPC UA移植,open62541移植
  • 【Tomcat与Servlet篇1】认识Tomcat与Maven
  • C++类和对象:拷贝构造函数和运算符重载
  • 【IntelliJ IDEA】idea plugins搜索不出来,如何找到插件的解决方案
  • 移动端自动化测试(一)appium环境搭建
  • 5 逻辑回归及Python实现
  • 技术干货 | Modelica建模秘籍之状态变量
  • LeetCode 2574. 左右元素和的差值
  • rollup环境配置
  • 二分查找与二分答案、递推与递归、双指针、并查集和单调队列
  • 如何进行域名购买,获取免费ssl证书,使用springboot绑定ssl证书
  • LabVIEW网络服务安全2
  • java动态代理
  • Python 简单可变、复杂可变、简单不可变、复杂不可变类型的copy、deepcopy的行为
  • QML Item
  • 使用xca工具生成自签证书
  • Unity IOS 通过命令行导出IPA
  • 「架构」全链路异步模式
  • CleanMyMac4.20最新版新增功能及电脑清理垃圾使用教程
  • Vue2的tsx开发入门完全指南
  • GLSL shader学习系列1-Hello World