C++ 集合

集合是 STL 容器,它们以排序的方式存储同一类型的唯一元素。

由于集合中的每个元素的唯一值,该值本身充当用于识别元素的键。


集合属性

C++ 集合具有以下属性。

1. 唯一元素

集合中的两个元素不能相等。

2. 不可变

我们可以向集合添加或删除元素,但不能更改现有元素的值。

3. 排序顺序

集合中的元素按照特定的严格弱序进行排序。

默认情况下,C++ 集合按升序排序,但我们可以选择更改它。

4. 关联性

集合的元素是使用它们的来引用的,而不是通过它们在容器中的位置。

这与 数组 不同,后者使用索引访问元素。


创建集合

要在 C++ 中实现集合,我们必须在程序中包含 <set> 头文件。

#include <set>

我们可以使用以下语法在 C++ 中创建集合

// declare a set
std::set<data_type> set_name = {key1, key2, key3, ...};

这里,

  • std::set - 声明一个类型为 set 的 STL 容器。
  • <data_type> - 要存储在集合中的值的数据类型
  • set_name - 分配给集合的唯一名称。
  • key1, key2, key3, ... - 要存储在集合中的键/值。

例如,

// initialize set with elements
std::set<int> my_set1 = {5, 3, 8, 1, 3};

// create an empty set
std::set<int> my_set2;

注意: 接下来,我们将使用 std 命名空间,这样我们就可以省略语法中的 std::


示例 1:创建集合

#include <iostream>
#include <set>
using namespace std;

int main() {

    set<int> my_set = {5, 3, 8, 1, 3};
    
    for(int val : my_set) {
        cout << val << " ";
    }

    return 0;
}

输出

1 3 5 8 

在这里,我们创建了一个类型为 int 的集合。

正如你所见,集合以排序的方式返回了值,并忽略了重复的 3

注意: 我们不能使用 [] 运算符 向集合添加元素。这样做会导致错误。

// this is not allowed in a set
my_set[4] = 4

按降序排序集合

要使集合的元素按降序排序,我们可以修改语法如下

set<int, greater<int>> my_set;

例如,

#include <iostream>
#include <set>
using namespace std;

int main() {

set<int, greater<int>> my_set = {5, 3, 8, 1, 3};
for(int val : my_set) { cout << val << " "; } return 0; }

输出

8 5 3 1 

从输出中可以看出,集合现在按降序返回元素。


C++ 集合方法

在 C++ 中,set 类提供了各种方法来执行集合中的各种操作。

操作 描述
insert() 将元素插入到集合中。
erase() 从集合中删除元素。
clear() 从集合中移除所有元素。
empty() 检查集合是否为空。
size() 返回集合的大小。

示例 2:向集合添加值

#include <iostream>     
#include <set>          
using namespace std;
   
int main () {

    set<int> my_set;

// add values to the set my_set.insert(10); my_set.insert(30); my_set.insert(20); my_set.insert(50); my_set.insert(40); my_set.insert(50);
// print the set elements for (int i : my_set) { cout << i << " "; } return 0; }

输出

10  20  30  40  50  

在这里,我们创建了一个类型为 int 的空集合 my_set,并使用 insert() 方法向其中添加了值。

请注意,重复的插入已被删除,并且尽管插入顺序不同,集合仍按升序排序。


示例 3:检查元素是否存在于集合中

#include <iostream>     
#include <set>          
using namespace std;
   
int main () {

    set<int> my_set;

    // add values to the set
    my_set.insert(10);
    my_set.insert(30);
    my_set.insert(20);
    my_set.insert(50);
    my_set.insert(40);
    my_set.insert(50);
    
    // check if 40 exists
    int num = 40;
    if(my_set.count(num) == 1) {
        cout << num << " exists." << endl;
    } else {
        cout << num << " doesn't exist." << endl;
    }
    
    // check if 60 exists
    num = 60;
    if(my_set.count(num) == 1) {
        cout << num << " exists." << endl;
    } else {
        cout << num << " doesn't exist." << endl;
    }
    
    return 0;
}

输出

40 exists.
60 doesn't exist.

在这里,我们创建了一个类型为 int 的空集合 my_set,并使用 insert() 方法向其中添加了值。

我们通过 my_set.count(num) 检查 4060 是否存在于集合 my_set 中,其中 num4060

如果 num 存在于 my_set 中,则 my_set.count(num) 返回 1,否则返回 0


示例 4:从集合中删除元素

#include <iostream>     
#include <set>          
using namespace std;
   
int main () {

    set<int> my_set = {10, 20, 30, 40};
    
    // set before deletion
    cout << "The set before deletion: ";
    for (int i : my_set) {
        cout << i << "  ";
    }

// delete values from the set my_set.erase(10); my_set.erase(20);
// set after deletion cout << "\nThe set after deletion: "; for (int i : my_set) { cout << i << " "; }
// delete all elements from the set my_set.clear();
// set after clearing all elements cout << "\nThe set after clearing all elements: "; for (int i : my_set) { cout << i << " "; } return 0; }

输出

The set before deletion: 10  20  30  40  
The set after deletion: 30  40
The set after clearing all elements:   

在这里,我们使用 erase() 方法删除单个集合元素。

然后,我们使用 clear() 方法删除集合中的所有元素。


示例 5:C++ 集合 empty() 和 size() 方法

在 C++ 中,empty()size() 方法被称为容量方法。

empty() 方法返回

  • 1 (true) - 如果集合为空
  • 0 (false) - 如果集合不为空

例如,

#include <iostream>     
#include <set>          
using namespace std;
   
int main () {

    set<int> my_set = {10, 20, 30};

    // set before clear
    cout << "The set before clear: ";
    for (int i : my_set) {
        cout << i << "  ";
    }
    
// check if the set is empty cout << "\nEmpty: " << my_set.empty() << endl; // check the size of the set cout << "Size: " << my_set.size() << endl;
// clear values from the set my_set.clear(); // set after clear cout << "\nThe set after clear: "; for (int i : my_set) { cout << i << " "; } // use the capacity methods again cout << "\nEmpty: " << my_set.empty() << endl; cout << "Size: " << my_set.size() << endl; return 0; }

输出

The set before clear: 10  20  30  
Empty: 0
Size: 3

The set after clear: 
Empty: 1
Size: 0

在这里,我们在清空集合内容之前和之后使用了 empty()size() 方法。

清空内容之前

  • empty() 返回 0,表示集合不为空
  • size() 返回 3,表示集合中有三个元素

清空内容之后

  • empty() 返回 1,表示集合为空
  • size() 返回 0,表示集合中没有元素

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战