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

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

定义于头文件 <set>
template<

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class set;
(1)
namespace pmr {

    template <class Key, class Compare = std::less<Key>>
    using set = std::set<Key, Compare, std::pmr::polymorphic_allocator<Key>>;

}
(2)(C++17 起)

std::set 是关联容器,含有 Key 类型对象的已排序集。用比较函数 比较 (Compare) 进行排序。搜索、移除和插入拥有对数复杂度。 set 通常以红黑树实现。

在每个标准库使用比较 (Compare) 概念的场所,用等价关系确定唯一性。不精确地说,若二个对象 ab 相互间既不比较大于亦不比较小于: !comp(a, b) && !comp(b, a) ,则认为它们等价。

std::set 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。

修改器

原位构造元素

std::set<Key,Compare,Allocator>::emplace

template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );

(C++11 起)

 若容器中无拥有该关键的元素,则插入以给定的 args 原位构造的新元素到容器。

细心地使用 emplace 允许在构造新元素的同时避免不必要的复制或移动操作。 准确地以与提供给 emplace 者相同的参数,通过 std::forward<Args>(args)... 转发调用新元素的构造函数。 即使容器中已有拥有该关键的元素,也可能构造元素,该情况下新构造的元素将被立即销毁。

没有迭代器或引用被非法化。

参数

args-要转发给元素构造函数的参数

返回值

返回由指向被插入元素,或若不发生插入则为既存元素的迭代器,和指代插入是否发生的 bool (若发生插入则为 true ,否则为 false )。

异常

若任何操作抛出异常,则此函数无效果。

复杂度

与容器大小成对数。

使用提示原位构造元素

std::set<Key,Compare,Allocator>::emplace_hint

template <class... Args>
iterator emplace_hint( const_iterator hint, Args&&... args );

(C++11 起)

插入新元素到容器中尽可能接近于恰在 hint 前的位置。原位构造元素,即不进行复制或移动操作。

以提供给函数的参数准确相同者,以 std::forward<Args>(args)... 转发调用元素的构造函数。

没有迭代器或引用被非法化。

参数

hint-指向新元素将插入到其前的位置的迭代器
args-转发给元素构造函数的参数

返回值

返回指向新插入元素的迭代器。

若因元素已存在而插入失败,则返回指向拥有等价关键的既存元素的迭代器。

异常

若任何操作抛出异常,则此函数无效果(强异常保证)。

复杂度

通常与容器大小成对数,但若新元素正好被插入到 hint 之前则为均摊常数。

 

交换内容

std::set<Key,Compare,Allocator>::swap

void swap( set& other );

(C++17 前)

void swap( set& other ) noexcept(/* see below */);

(C++17 起)

将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。

所有迭代器和引用保持合法。尾后迭代器被非法化。

Pred 对象必须可交换 (Swappable) ,并用非成员 swap 的非限定调用交换它们。

若 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 为 true ,则用非成员 swap 的非限定调用交换分配器。否则,不交换它们(且若 get_allocator() != other.get_allocator() ,则行为未定义)。

(C++11 起)

参数

other-要与之交换内容的容器

返回值

(无)

异常

任何 Compare 对象交换所抛的异常。

(C++17 前)
noexcept 规定:  

noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Compare>::value)

(C++17 起)

复杂度

常数。

 

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <set>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;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{auto generate = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};std::set<Cell> set1;for (size_t index = 0; index < 5; index++){//插入以给定的 args 原位构造的新元素到容器。set1.emplace(std::rand() % 10 + 100, std::rand() % 10 + 100);std::cout << "set1:    ";std::copy(set1.begin(), set1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;}std::cout << std::endl;std::set<Cell> set2;for (size_t index = 0; index < 5; index++){//插入新元素到尽可能靠近恰在 hint 前的位置。原位构造元素,即不进行复制或移动操作。set2.emplace_hint(set2.cbegin(), std::rand() % 10 + 100, std::rand() % 10 + 100);std::cout << "set2:    ";std::copy(set2.begin(), set2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;}std::cout << std::endl;std::cout << "swap before:" << std::endl;std::set<Cell> set3{generate(), generate(), generate(), generate(), generate()};std::cout << "set3:    ";std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::set<Cell> set4{generate(), generate(), generate(), generate(), generate()};std::cout << "set4:    ";std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。set3.swap(set4);std::cout << "swap after:" << std::endl;std::cout << "set3:    ";std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "set4:    ";std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;return 0;
}

输出

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

相关文章:

  • linux服务器断电重启后,发现时间误差八小时
  • 兼容人大金仓,异常信息报错解决大全
  • 短睡眠 堀大辅 超短眠 人生更丰富
  • 私有GitLab仓库 - 本地搭建GitLab私有代码仓库并随时远程访问「内网穿透」
  • Debezium系列之:Debezium镜像仓库Quay.io,使用Debezium镜像仓库的方法和案例
  • 文心一言和ChatGPT最全对比
  • 龙芯2K1000实战开发-平台介绍
  • C++ map用法总结(整理)
  • 面向对象的第二个基本特征:继承011026
  • 机器学习项目实战-能源利用率 Part-3(特征工程与特征筛选)
  • WebSocket的那些事(2-实操篇)
  • BurpSuite—-Target模块(目标模块)
  • 部门来了个测试开发,听说是00后,上来一顿操作给我看呆了...
  • Godot引擎 4.0 文档 - 入门介绍 - Godot简介
  • 数据通信基础 - 码元速率 和 数据速率 详解
  • 听我一句劝,别去外包,干了三年,废了....
  • 全域兴趣电商:国货品牌的新策略、新玩法
  • 嵌入式 Linux 入门(十一、make 和 MakeFile)
  • Serverless冷扩机器在压测中被击穿问题 | 京东云技术团队
  • 数仓中指标-标签,维度-度量,自然键-代理键等各名词深度解析
  • Baumer工业相机堡盟工业相机使用BGAPI SDK将图像数据转换为Bitmap的几种方式(C++)(Mono)
  • C++笔试笔记2
  • 手写Spring框架
  • 微服务学习笔记--(Docker)
  • ChatGPT 国内版免费
  • 推荐5个免费好用的UI模板网站!
  • linux 安装 maven 3.8 版本
  • Redis的三种持久化策略及选取建议
  • 力扣LCP 33. 蓄水
  • 内网渗透(八十一)之搭建Exchange服务器