#include <set>

C++ 标准库中的 <set> 是一个关联容器,它存储了一组唯一的元素,并按照一定的顺序进行排序。

// <set> -*- C++ -*-

// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/** @file include/set
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_SET
#define _GLIBCXX_SET 1

#pragma GCC system_header

#include <bits/stl_tree.h>
#include <bits/stl_set.h>
#include <bits/stl_multiset.h>
#include <bits/range_access.h>
#include <bits/erase_if.h>

#ifdef _GLIBCXX_DEBUG
# include <debug/set>
#endif

#if __cplusplus >= 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  namespace pmr
  {
    template<typename _Tp> class polymorphic_allocator;
    template<typename _Key, typename _Cmp = std::less<_Key>>
      using set = std::set<_Key, _Cmp, polymorphic_allocator<_Key>>;
    template<typename _Key, typename _Cmp = std::less<_Key>>
      using multiset = std::multiset<_Key, _Cmp, polymorphic_allocator<_Key>>;
  } // namespace pmr
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++17

#if __cplusplus > 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  template<typename _Key, typename _Compare, typename _Alloc,
	   typename _Predicate>
    inline typename set<_Key, _Compare, _Alloc>::size_type
    erase_if(set<_Key, _Compare, _Alloc>& __cont, _Predicate __pred)
    { return __detail::__erase_nodes_if(__cont, __pred); }

  template<typename _Key, typename _Compare, typename _Alloc,
	   typename _Predicate>
    inline typename multiset<_Key, _Compare, _Alloc>::size_type
    erase_if(multiset<_Key, _Compare, _Alloc>& __cont, _Predicate __pred)
    { return __detail::__erase_nodes_if(__cont, __pred); }
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++20

#endif /* _GLIBCXX_SET */
  • insert(元素): 插入一个元素。
  • erase(元素): 删除一个元素。
  • find(元素): 查找一个元素。
  • size(): 返回容器中元素的数量。
  • empty(): 检查容器是否为空。
### 3.1 set容器的基本作用 在C++中,`#include <set>` 引入了标准库中的 `set` 容器[^4]。`set` 是一种关联容器,用于存储唯一且有序的元素。其核心特性包括: - **唯一性**:集合中的元素值是唯一的,不允许重复。 - **有序性**:元素默认按照升序排列,这种排序是自动维护的。 - **不可修改性**:集合中的元素值不能直接修改,只能通过删除旧值并插入新值的方式来更新。 `set` 的内部实现基于红黑树(Red-Black Tree),这使得插入、删除和查找操作的时间复杂度均为 `O(log n)`。 ### 3.2 基本用法与操作 #### 3.2.1 声明与初始化 `set` 可以通过以下方式声明: ```cpp #include <set> std::set<int> s; ``` 也可以使用初始化列表或从数组中构造: ```cpp int myints[] = {75, 23, 65, 42, 13}; std::set<int> myset(myints, myints + 5); ``` 该代码将创建一个包含 `13, 23, 42, 65, 75` 的集合[^2]。 #### 3.2.2 插入元素 使用 `insert()` 方法向集合中添加元素: ```cpp s.insert(8); s.insert(1); s.insert(12); s.insert(6); s.insert(8); // 重复元素不会插入 ``` 由于 `set` 中的元素必须唯一,因此第二次插入 `8` 将被忽略[^3]。 #### 3.2.3 遍历集合 可以通过迭代器遍历 `set` 中的元素: ```cpp for (std::set<int>::iterator it = s.begin(); it != s.end(); ++it) { std::cout << ' ' << *it; } ``` 输出结果将按照升序排列,如 `1 6 8 12`。 #### 3.2.4 查找与删除 - **查找元素** 使用 `find()` 方法,返回指向该元素的迭代器,若未找到则返回 `end()`: ```cpp std::set<int>::iterator it = s.find(8); if (it != s.end()) { std::cout << "Found: " << *it << std::endl; } ``` - **删除元素** 使用 `erase()` 方法,可以删除特定值或特定迭代器指向的元素: ```cpp s.erase(8); // 删除值为8的元素 s.erase(it); // 删除迭代器it指向的元素 ``` #### 3.2.5 其他常用操作 - `size()`:返回集合中的元素个数。 - `empty()`:判断集合是否为空。 - `clear()`:清空集合中的所有元素。 - `count(x)`:返回集合中等于 `x` 的元素个数(在 `set` 中,结果只能是 `0` 或 `1`)。 - `lower_bound(x)` 和 `upper_bound(x)`:用于查找集合中大于等于 `x` 和严格大于 `x` 的最小元素。 ### 3.3 set 与其他容器的区别 与 `vector` 和 `list` 等序列容器不同,`set` 是一种**有序且唯一**的集合,适用于需要快速查找、插入和删除的场景。与 `multiset` 相比,`set` 不允许重复元素,而 `multiset` 可以包含多个相同值的元素。 此外,`map` 和 `set` 类似,但 `map` 存储的是键值对,而 `set` 只存储单一的值。 ### 3.4 示例代码 以下是一个完整的 `set` 使用示例: ```cpp #include <iostream> #include <set> using namespace std; int main() { set<int> s; s.insert(8); s.insert(1); s.insert(12); s.insert(6); s.insert(8); // 重复元素不会插入 cout << "Set contains: "; for (set<int>::iterator it = s.begin(); it != s.end(); ++it) cout << ' ' << *it; cout << endl; if (s.find(8) != s.end()) cout << "8 is found in the set" << endl; s.erase(8); cout << "After erasing 8, set contains: "; for (set<int>::iterator it = s.begin(); it != s.end(); ++it) cout << ' ' << *it; cout << endl; return 0; } ``` 输出结果为: ``` Set contains: 1 6 8 12 8 is found in the set After erasing 8, set contains: 1 6 12 ``` 该示例演示了插入、查找和删除操作的使用[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值