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

news2024/10/1 15:23:42
定义于头文件 <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>::clear

void clear() noexcept;

(C++11 起)

从容器擦除所有元素。此调用后 size() 返回零。

非法化任何指代所含元素的引用、指针或迭代器。可能亦非法化尾后迭代器。

参数

(无)

返回值

(无)

复杂度

与容器大小,即元素数成线性。

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

 

擦除元素

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

iterator erase( const_iterator pos );

(1)(C++11 起)

iterator erase( const_iterator first, const_iterator last );

(2)(C++11 起)

size_type erase( const key_type& key );

(3)(C++11 起)

 

从容器移除指定的元素。

1) 移除位于 pos 的元素。

2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。

3) 移除关键等于 key 的元素(若存在一个)。

到被擦除元素的引用和迭代器被非法化。其他迭代器和引用不被非法化。

迭代器 pos 必须合法且可解引用。从而 end() 迭代器(合法,但不可解引用)不能用作 pos 所用的值。

保留未被擦除的元素顺序(这使得可能在迭代通过容器时擦除单独的元素)。

(C++14 起)

参数

pos-指向要移除的元素的迭代器
first, last-要移除的元素范围
key-要移除的元素关键值

返回值

1-2) 后随最后被移除的元素的迭代器。

3) 被移除的元素数。

异常

1,2) (无)

3) 任何 Compare 对象所抛的异常

复杂度

给定 unordered_set 的实例 c

1) 平均情况:常数,最坏情况: c.size()

2) 平均情况: std::distance(first, last) ,最坏情况: c.size()

3) 平均情况: c.count(key) ,最坏情况: c.size()

调用示例

#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::unordered_set<Cell, CHash, CEqual> unordered_set1;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set1.insert({generate(), generate(), generate(), generate(), generate(), 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 << "unordered_set1 empty :    " << unordered_set1.empty() << std::endl;
    //从容器擦除所有元素。此调用后 size() 返回零。
    unordered_set1.clear();
    std::cout << "unordered_set1 empty :    " << unordered_set1.empty() << std::endl;
    std::cout << "unordered_set1 size  :    " << unordered_set1.size() << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set2;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set2.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set2:   ";
    std::copy(unordered_set2.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    size_t usize = unordered_set2.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //从容器移除指定的元素。1) 移除位于 pos 的元素。
        std::unordered_set<Cell, CHash, CEqual>::iterator rit = unordered_set2.erase(unordered_set2.cbegin());
        std::cout << "unordered_set2 erase iterator :   " << *rit << std::endl;
    }
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set3;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set3.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set3:   ";
    std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_set<Cell, CHash, CEqual>::const_iterator bcit = unordered_set3.cbegin();
    bcit++;
    std::unordered_set<Cell, CHash, CEqual>::const_iterator ecit = bcit;
    ecit++, ecit++;
    unordered_set3.erase(bcit, ecit);
    std::cout << "unordered_set3:   ";
    std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set4;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set4.insert({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;

    usize = unordered_set4.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //从容器移除指定的元素。3) 移除关键等于 key 的元素(若存在一个)。
        size_t esize = unordered_set4.erase(*unordered_set4.cbegin());
        std::cout << "unordered_set4 erase iterator esize:   " << esize << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/370172.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Rust 日报】2023-2-24 Dioxus 0.3 发布,巨大的更新

ascii-d - 画ASCII示意图的工具Rust写的画ASCII示意图的工具。支持各大平台。程序员的最爱啊。https://github.com/huytd/ascii-d/raw/master/_meta/toolbar-final.gifDioxus 0.3 发布&#xff0c;巨大的更新Dioxus 是新出的与 Yew 类似的 Rust Web 前端框架&#xff08;为什么…

【sciter】sciter数据可视化

一、柱状图 <div class="bar-chart item"></div> <!-- bar-chart --> <script type

【华为OD机试模拟题】用 C++ 实现 - 找单词(2023.Q1)

最近更新的博客 华为OD机试 - 入栈出栈(C++) | 附带编码思路 【2023】 华为OD机试 - 箱子之形摆放(C++) | 附带编码思路 【2023】 华为OD机试 - 简易内存池 2(C++) | 附带编码思路 【2023】 华为OD机试 - 第 N 个排列(C++) | 附带编码思路 【2023】 华为OD机试 - 考古…

【华为OD机试模拟题】用 C++ 实现 - RSA 加密算法(2023.Q1)

最近更新的博客 华为OD机试 - 入栈出栈(C++) | 附带编码思路 【2023】 华为OD机试 - 箱子之形摆放(C++) | 附带编码思路 【2023】 华为OD机试 - 简易内存池 2(C++) | 附带编码思路 【2023】 华为OD机试 - 第 N 个排列(C++) | 附带编码思路 【2023】 华为OD机试 - 考古…

猜数字游戏——C++

我们在有了一定的C基础了以后&#xff0c;简单的实现一个案例&#xff08;其实只要会while循环结构就行了&#xff09;&#xff0c;我们本章内容会实现猜数字游戏&#xff0c;大家有什么语法疑问可以看看我写的&#xff1a;C快速入门_染柒_GRQ的博客-CSDN博客&#xff0c;该博客…

【算法】最短路算法

&#x1f600;大家好&#xff0c;我是白晨&#xff0c;一个不是很能熬夜&#x1f62b;&#xff0c;但是也想日更的人✈。如果喜欢这篇文章&#xff0c;点个赞&#x1f44d;&#xff0c;关注一下&#x1f440;白晨吧&#xff01;你的支持就是我最大的动力&#xff01;&#x1f4…

金三银四吃透这份微服务笔记,面试保准涨10K+

很多人对于微服务技术也都有着一些疑虑&#xff0c;比如&#xff1a; 微服务这技术虽然面试的时候总有人提&#xff0c;但作为一个开发&#xff0c;是不是和我关系不大&#xff1f;那不都是架构师的事吗&#xff1f;微服务不都是大厂在玩吗&#xff1f;我们这个业务体量用得着…

2023年湖北住建厅七大员建筑八大员怎么报考?启程别

2023年湖北住建厅七大员建筑八大员怎么报考&#xff1f;启程别 建筑施工企业关键技术岗位人员可以叫七大员也可以叫八大员&#xff0c;施工现场专业人员&#xff0c;从事相关岗位人员都应该持证上岗。 为什么有的叫七大员&#xff1f;有的叫八大员呢&#xff1f;甚至还有五大员…

Web Spider Ast-Hook 浏览器内存漫游-数据检索

文章目录一、资源下载二、通过npm安装anyproxy模块三、anyproxy的介绍以及基本使用1. anyproxy的功能介绍2. anyproxy的基本使用四、给浏览器挂代理五、实操极验demo案例总结提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、资源下载 Github&#x…

【数通网络交换基础梳理1】二层交换机、以太网帧、MAC地址详解及数据帧转发原理(爆炸细)

一、网络模型 万年不变&#xff0c;先从模型结构分析&#xff0c;现在大家熟知的网络模型有两种。第一种是&#xff0c;OSI七层模型&#xff0c;第二种是TCP/IP模型。在实际运用中&#xff0c;参考更多的是TCP/IP模型。 OSI七层模型 TCP/IP模型 不需要全部理解&#xff0c;…

从 JDK 8 到 JDK 18,Java 垃圾回收的十次进化

经历了数千次改进&#xff0c;Java 的垃圾回收在吞吐量、延迟和内存大小方面有了巨大的进步。 2014 年3 月 JDK 8 发布&#xff0c;自那以来 JDK 又连续发布了许多版本&#xff0c;直到今日的 JDK 18 是 Java 的第十个版本。借此机会&#xff0c;我们来回顾一下 HotSpot JVM 的…

Python稀疏矩阵最小二乘法

文章目录最小二乘法返回值测试最小二乘法 scipy.sparse.linalg实现了两种稀疏矩阵最小二乘法lsqr和lsmr&#xff0c;前者是经典算法&#xff0c;后者来自斯坦福优化实验室&#xff0c;据称可以比lsqr更快收敛。 这两个函数可以求解AxbAxbAxb&#xff0c;或arg min⁡x∥Ax−b…

行业观察 | SoC

本文对 SoC 进行不完全总结。 更新&#xff1a;2022 / 02 / 25 行业观察 | SoC总览概念组成周期原因产业链及市场上游下游产品厂商SoC V.S MCUMCUSoCMCU V.S SoC参考链接总览 概念 SoC 1‘ 2’ 3’ 4, 全称 System On Chip&#xff0c;又称 系统级芯片、片上系统&#xff0c…

顺序表(超详解哦)

全文目录引言顺序表定义静态顺序表动态顺序表动态顺序表的接口实现顺序表的初始化与销毁顺序表尾插/尾删顺序表头插/头删顺序表在pos位置插入/删除顺序表的打印顺序表中的查找总结引言 在生产中&#xff0c;为了方便管理数据&#xff0c;我们经常会需要将一些数据连续的存储起…

MyBatis——创建与使用

概念 当我们使用传统的jdbc进行数据库与程序的连接时&#xff0c;每一个操作都需要写一条sql语句&#xff0c;并且没法调试和修改 jdbc连接数据库流程&#xff1a; 创建数据库连接池DataSource获取数据库连接Connection执行带占位符的sql语句通过Connection创建操作对象Stat…

【LeetCode】剑指 Offer(8)

目录 题目&#xff1a;剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 - 力扣&#xff08;Leetcode&#xff09; 题目的接口&#xff1a; 解题思路&#xff1a; 代码&#xff1a; 过啦&#xff01;&#xff01;&#xff01; 题目&#xff1a;剑指 Offer 24. 反转链表 - …

HTTP 返回码

HTTP 返回码1XX 指示信息2XX 成功3XX 重定向301 Moved Permanently302 Found304 Not Modified4XX 客户端错误400 Bad Request401 Unauthorized403 Forbidden404 Not Found405 Method Not Allowed411 Length Requied413 Request Entity Too Large414 Request Uri Too Long5XX 服…

学习 Python 之 Pygame 开发魂斗罗(一)

学习 Python 之 Pygame 开发魂斗罗&#xff08;一&#xff09;Pygame回忆Pygame1. 使用pygame创建窗口2. 设置窗口背景颜色3. 获取窗口中的事件4. 在窗口中展示图片(1). pygame中的直角坐标系(2). 展示图片(3). 给部分区域设置颜色5. 在窗口中显示文字6. 播放音乐7. 图片翻转与…

php使用wangeditor实现富文本

官网参考连接&#xff1a;https://www.wangeditor.com/v5/getting-started.html样式&#xff1a;前端代码&#xff1a;搭建前端部分样式&#xff1a;<tr><td><span style"color:red">*</span>内容</td><td colspan"20"&g…

锁屏面试题百日百刷-Hive篇(二)

锁屏面试题百日百刷&#xff0c;每个工作日坚持更新面试题。锁屏面试题app、小程序现已上线&#xff0c;官网地址&#xff1a;https://www.demosoftware.cn/#/introductionPage。已收录了每日更新的面试题的所有内容&#xff0c;还包含特色的解锁屏幕复习面试题、每日编程题目邮…