unordered_set容器函数说明

本文详细介绍了C++标准库中的unordered_set容器的各种构造方法,包括空构造、拷贝构造、移动构造等。此外,还涵盖了迭代器的使用,如begin()和end(),以及查询容器状态的方法,如empty()和size()。文章还讨论了插入、删除和查找元素的关键函数,如insert、erase和find等。

构造函数

  • unordered_set:构造一个空的unordered_set容器。
  • unordered_set(const unordered_set& other):拷贝构造函数,构造一个unordered_set容器,并用另一个unordered_set容器的副本初始化。
  • unordered_set(unordered_set&& other) noexcept:移动构造函数,构造一个unordered_set容器,并用另一个unordered_set容器的元素初始化,另一个容器的内容将被移动。
  • unordered_set(size_type count):构造一个包含count个元素的unordered_set容器。
  • unordered_set(size_type count, const key_type& key, const hasher& hash_fn = hasher(), const key_equal& equal_fn = key_equal()):构造一个包含count个重复的key元素的unordered_set容器。
  • template< class InputIt > unordered_set(InputIt first, InputIt last, size_type count = 0, const hasher& hash_fn = hasher(), const key_equal& equal_fn = key_equal()):构造一个包含范围在[first, last)之间的元素的unordered_set容器。

迭代器

  • begin():返回一个指向unordered_set容器第一个元素的迭代器。
  • end():返回一个指向unordered_set容器尾元素下一个位置的迭代器。
  • cbegin() const:返回一个指向unordered_set容器第一个元素的const迭代器。
  • cend() const:返回一个指向unordered_set容器尾元素下一个位置的const迭代器。

容量

  • empty() const:返回一个bool值,指示unordered_set容器是否为空。
  • size() const:返回unordered_set容器中元素的数量。
  • max_size() const:返回unordered_set容器可以容纳的最大元素数。

修改器

  • insert(const value_type& value):将元素value插入unordered_set容器中。
  • insert(value_type&& value):将元素value插入unordered_set容器中,使用移动语义。
  • insert(InputIt first, InputIt last):插入来自范围[first, last)的元素到unordered_set容器中。
  • insert(std::initializer_list<value_type> ilist):从initializer_list中插入元素到unordered_set容器中。
  • emplace(Args&&... args):通过参数args构造元素并将其插入到unordered_set容器中。
  • erase(const_iterator position):从unordered_set容器中删除迭代器position所指向的元素。
  • erase(const key_type& key):从unordered_set容器中删除所有等于key的元素。
  • erase(first, last):从unordered_set容器中删除范围[first, last)的元素。
  • clear():从unordered_set容器中删除所有元素。

查找

  • find(const key_type& key):返回指向键值为key的元素的迭代器。如果未找到key,则返回end迭代器。
  • count(const key_type& key):统计键为key的元素个数,返回值为0或1,因为该容器不允许有重复元素。
  • equal_range(const key_type& key):返回键值等于key的元素范围,返回值为一个pair,其两个成员都是迭代器类型,表示范围的起始位置和结束位置。
### C++ 中 `set` 与 `unordered_set` 的 `insert` 函数使用方法及差异 #### `set` 容器的 `insert` 函数 `set` 是一个有序的关联容器,内部元素按照升序排列。`set` 的 `insert` 函数支持多种插入方式,包括插入单个元素、插入范围内的元素以及通过位置提示插入元素。 - **插入单个元素** 插入一个元素时,`set` 会自动确保元素的唯一性,并按照排序规则插入到正确的位置。 ```cpp std::set<int> s; s.insert(10); // 插入单个元素 s.insert(5); // 插入元素 5,并自动排序 ``` - **插入带有提示的位置** 提供一个迭代器作为插入位置的提示,虽然提示位置不保证插入效率的提升,但可以优化某些特定场景的性能。 ```cpp auto it = s.find(10); s.insert(it, 15); // 提示插入位置在 it 附近 ``` - **插入范围内的元素** 可以将另一个容器元素插入到 `set` 中。 ```cpp std::vector<int> vec = {20, 25, 30}; s.insert(vec.begin(), vec.end()); // 插入 vector 中的元素 ``` - **返回值** 插入单个元素时,`insert` 返回一个 `std::pair<iterator, bool>`,其中 `bool` 表示插入是否成功(若元素已存在,则插入失败)[^1]。 #### `unordered_set` 容器的 `insert` 函数 `unordered_set` 是一个无序的关联容器,内部使用哈希表实现,因此插入元素时不会按照顺序排列,而是根据哈希值分布到不同的桶中。 - **插入单个元素** ```cpp std::unordered_set<int> us; us.insert(10); // 插入单个元素 us.insert(5); // 插入元素 5,无序排列 ``` - **插入带有提示的位置** `unordered_set` 也支持插入时提供位置提示,但该提示对性能的提升作用有限,因为哈希表的插入逻辑与有序容器不同。 ```cpp auto it = us.find(10); us.insert(it, 15); // 提示插入位置在 it 附近 ``` - **插入范围内的元素** ```cpp std::vector<int> vec = {20, 25, 30}; us.insert(vec.begin(), vec.end()); // 插入 vector 中的元素 ``` - **返回值** 与 `set` 类似,`unordered_set` 的 `insert` 返回值也是 `std::pair<iterator, bool>`,用于判断插入是否成功[^2]。 #### `set` 与 `unordered_set` 的 `insert` 函数差异总结 1. **排序与无序** `set` 中的元素是按照升序排列的,而 `unordered_set` 中的元素是无序的,基于哈希值分布[^1]。 2. **插入效率** `set` 的插入操作时间复杂度为 $O(\log n)$,而 `unordered_set` 的平均时间复杂度为 $O(1)$,但在最坏情况下可能退化为 $O(n)$(例如哈希冲突较多时)[^2]。 3. **插入位置提示** `set` 的插入位置提示可以显著提高插入效率,因为它可以减少查找插入点的时间。而 `unordered_set` 的提示插入效率提升有限,因为其插入逻辑主要依赖哈希值[^3]。 4. **元素唯一性** 两者都保证元素的唯一性,插入重复元素时会失败,并返回 `false`。 5. **适用场景** 如果需要有序的元素存储,应选择 `set`;如果对顺序无要求且追求更高的插入效率,可以选择 `unordered_set`。 #### 示例代码 以下是一个简单的示例,演示 `set` 和 `unordered_set` 的 `insert` 使用方法: ```cpp #include <iostream> #include <set> #include <unordered_set> #include <vector> int main() { // set 示例 std::set<int> s; s.insert(10); s.insert(5); s.insert(15); std::cout << "set elements: "; for (const auto& elem : s) { std::cout << elem << " "; } std::cout << std::endl; // unordered_set 示例 std::unordered_set<int> us; us.insert(10); us.insert(5); us.insert(15); std::cout << "unordered_set elements: "; for (const auto& elem : us) { std::cout << elem << " "; } std::cout << std::endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇宙无穷,盈虚有数

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值