当前位置: 首页 > 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>::operator=

set& operator=( const set& other );

(1)

set& operator=( set&& other );

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

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

(C++17 起)

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) 通常为 O(NlogN) ,其中 N 为 size() + ilist.size() 。若 ilist 相对于 value_comp() 已排序则为线性。

异常

2)noexcept 规定:  

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

(C++17 起)

注意

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


返回相关的分配器

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

allocator_type get_allocator() const;

返回与容器关联的分配器。

参数

(无)

返回值

关联的分配器。

复杂度

常数。

调用示例

#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{generate(), generate(), generate(), generate(), generate()};std::cout << "set1:    ";std::copy(set1.begin(), set1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//1) 复制赋值运算符。以 other 的副本替换内容。std::set<Cell> set2 = set1;std::cout << "set2:    ";std::copy(set2.begin(), set2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。std::set<Cell> set3 = std::move(set1);std::cout << "set1 is empty:   " << set1.empty() << std::endl;std::cout << "set3:    ";std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//3) 以 initializer_list ilist 所标识者替换内容。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;return 0;
}

输出

 

 

 

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

相关文章:

  • ChatGPT详细介绍
  • 【算法】【算法杂谈】让[0,x)区间上的出现概率变为x^k
  • 【2023华为OD笔试必会25题--C语言版】《21 对称美学》——字符串、递归
  • 为减少来自环境使用的无线传感器网络的传输次数而开发的方法(Matlab代码实现)
  • springboot+vue滴答拍摄影项目(源码+文档)
  • SQL基础培训13-索引和优化
  • 拥抱5G发展机遇,从边缘计算上车
  • “前端”工匠系列(二):合格的工匠,怎么做好价值落地 | 京东云技术团队
  • Oracle11g下载与安装
  • 考研复试-软件工程
  • 软件测试选择题
  • 有限合伙企业与有限公司的区别
  • 从洛克菲勒思想中洞悉的财富秘密
  • 如何训练自己的大型语言模型
  • Java中的SLF4J是什么?如何使用SLF4J进行日志管理
  • PHP程序员面对的压力大不大?我来聊聊程序员转行的就业方向
  • 牛客网专项练习Pytnon分析库(十)
  • leecode654——最大二叉树
  • 【笔试强训选择题】Day12.习题(错题)解析
  • 边缘计算与开放源代码的完美结合
  • 边缘计算网关在储能系统中的应用——提高储能系统的安全性和稳定性
  • 【FMC136】AD9467之4通道 250MSPS 采样率16位AD 采集子卡模块得设计原理图中文资料
  • 抖音SEO矩阵系统源码开发(一)
  • Mysql实现对某一字段排序并将排名写入另一字段
  • vector容器 [上]
  • React Native技术探究:开发高质量的跨平台移动应用的秘诀
  • C语言函数大全-- w 开头的函数(2)
  • kafka启动创建topic报错:zookeeper is not a recognized option
  • 11个超好用的SVG编辑工具
  • 低代码平台:10分钟从入门到原理