C++黑马程序员——P223-226. set容器 构造和赋值,大小和交换,插入和删除,查找和统计

发布时间 2023-10-11 18:30:44作者: 我会变强的
  • P223. set容器——构造和赋值
  • P224. set容器——大小和交换
  • P225. set容器——插入和删除
  • P226. set容器——查找和统计
  • P223. set容器 构造和赋值
  • 特点:所有元素都会在插入时自动被排序
  • 本质:set/multiset 属于关联式容器,底层结构是用二叉树实现。
  • set 和 multiset 的区别
    • set 不允许容器中有重复的元素
    • multiset 允许容器中有重复的元素
  • 使用时需要 #include <set>  
  • 构造和赋值

  

  • 示例
 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 
 5 // set容器构造和赋值
 6 
 7 void printSet(set<int>& s) {
 8     for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
 9         cout << *it << " ";
10     }
11     cout << endl;
12 }
13 
14 void test01() {
15     set<int>s1;
16     // 插入数据 只有insert方式
17     s1.insert(10);
18     s1.insert(30);
19     s1.insert(40);
20     s1.insert(30);
21     s1.insert(20);
22 
23     // 遍历容器
24     printSet(s1);
25 
26     // 拷贝构造
27     set<int>s2(s1);
28     cout << "s2:" << endl;
29     printSet(s2);
30 
31     // 赋值
32     set<int>s3;
33     s3 = s2;
34     cout << "s3:" << endl;
35     printSet(s3);
36 }
37 
38 int main() {
39     test01();
40 
41     return 0;
42 }

res:

  

 

  • P224. set容器 大小和交换

(不允许重新指定大小,即不允许 resize()

  

—————————————————————————————————————————————————

 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 
 5 void printSet(set<int>& s) {
 6     for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
 7         cout << *it << " ";
 8     }
 9     cout << endl;
10 }
11 
12 // set容器 大小和交换
13 // 大小
14 void test01() {
15     set<int>s1;
16     s1.insert(30);
17     s1.insert(40);
18     s1.insert(10);
19     s1.insert(20);
20 
21     printSet(s1);
22 
23     // 判断是否为空
24     if (s1.empty()) {
25         cout << "s1为空" << endl;
26     }
27     else {
28         cout << "s1不为空" << endl;
29         cout << "s1的大小为:" << s1.size() << endl;
30     }
31 }
32 
33 // 交换
34 void test02() {
35     set<int>s1;
36     s1.insert(30);
37     s1.insert(40);
38     s1.insert(10);
39     s1.insert(20);
40 
41     set<int>s2;
42     s2.insert(300);
43     s2.insert(400);
44     s2.insert(100);
45     s2.insert(200);
46 
47     cout << "交换前:" << endl;
48     cout << "s1: ";
49     printSet(s1);
50     cout << "s2: ";
51     printSet(s2);
52 
53     s1.swap(s2);
54     cout << "交换后:" << endl;
55     cout << "s1: ";
56     printSet(s1);
57     cout << "s2: ";
58     printSet(s2);
59 }
60 
61 int main() {
62     cout << "test01():" << endl;
63     test01();
64     cout << endl;
65     cout << "test02()" << endl;
66     test02();
67     return 0;
68 }

res:

  

  • P225. set容器 插入和删除

  

—————————————————————————————————————————————————

 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 
 5 void printSet(set<int>& s) {
 6     for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
 7         cout << *it << " ";
 8     }
 9     cout << endl;
10 }
11 
12 // set容器 插入和删除
13 void test01() {
14     set<int>s1;
15     // 插入
16     s1.insert(10);
17     s1.insert(40);
18     s1.insert(30);
19     s1.insert(20);
20 
21     // 遍历
22     printSet(s1);
23 
24     // 删除
25     s1.erase(s1.begin());    // 通过迭代器删
26     printSet(s1);
27 
28     s1.erase(30);    // 直接删元素
29     printSet(s1);
30 
31     // 清空
32     // s1.erase(s1.begin(), s1.end());    // 删除两个迭代器之间的元素
33     s1.clear();
34     printSet(s1);
35 }
36 
37 int main() {
38     test01();
39     return 0;
40 }

res:

  

  • P226. set容器 查找和统计

  

—————————————————————————————————————————————————

 

 1 #include <iostream>
 2 #include <set>
 3 using namespace std;
 4 
 5 // set容器 查找和统计
 6 // 查找
 7 void test01() {
 8     set<int>s1;
 9     s1.insert(10);
10     s1.insert(20);
11     s1.insert(30);
12     s1.insert(40);
13 
14     set<int>::iterator pos = s1.find(30);
15     if (pos != s1.end()) {
16         cout << "找到元素:" << *pos << endl;
17     }
18     else {
19         cout << "未找到元素" << endl;
20     }
21 }
22 
23 // 统计
24 void test02() {
25     set<int>s1;
26     s1.insert(10);
27     s1.insert(20);
28     s1.insert(30);
29     s1.insert(40);
30 
31     // 统计30的个数
32     int num = s1.count(30);    // 对于set而言,统计结果要么是0,要么是1(multiset允许插入重复元素)
33     cout << "num = " << num << endl;
34 }
35 
36 int main() {
37     test01();
38     cout << endl;
39     test02();
40     return 0;
41 }

res:

  

(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)