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

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

时间:2023-10-11 18:33:05浏览次数:51  
标签:insert set cout 容器 s1 30 C++ P223

  • 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:

  

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

标签:insert,set,cout,容器,s1,30,C++,P223
From: https://www.cnblogs.com/wjjgame/p/17728363.html

相关文章

  • ADO.NET读取MySQL数据库的三种方式:DataReader、DataSet、DataView
    https://blog.csdn.net/lilongsy/article/details/127351421ADO.NET读取MySQL数据库有多种方式:DataReader、DataSet、DataView。Command对象的ExecuteScalar方法查询数据库获取某个单个值,但是如果获取多行、多列可以用ExcecuteReader,ExcecuteReader返回一个DataReader的数据流对......
  • 1——of C++ and Java togather
    因为那个C++最全的笔记是从第18课开始做(笔者说18课之前都很基础),所以这里就对前18课的知识做个笔记总结C++的工作过程这里提到的C++工作过程主要涉及两个:编译与链接之前考研时候学到,(在组成原理的某个章节),计算机的工作过程其实就涉及“将源程序转换成可执行文件”,与其中便......
  • C++ - 基于范围的 for 循环
    在C++98/03中,不同的容器和数组遍历的方式不尽相同,写法不统一,也不够简洁,而C++11基于范围的for循环可以以简洁、统一的方式来遍历容器和数组,用起来也更方便了。1.for循环新语法在介绍新语法之前,先来看一个使用迭代器遍历容器的例子:#include<iostream>#include<vector>......
  • C++ - move()函数
    C++11标准中借助右值引用可以为指定类添加移动构造函数,这样当使用该类的右值对象(可以理解为临时对象)初始化同类对象时,编译器会优先选择移动构造函数。注意,移动构造函数的调用时机是:用同类的右值对象初始化新对象。那么,用当前类的左值对象(有名称,能获取其存储地址的实例对象)初始化......
  • 如何解决小程序打开授权报错openSetting:fail can only be invoked by user TAP gestu
    要解决这个问题,你需要在页面上为openSetting接口添加一个点击事件。根据微信小程序的文档,openSetting接口只能通过用户点击行为(如tap事件)来触发。以下是一个简单的示例代码:<!--index.wxml--><view><buttonbindtap="openSetting">打开授权设置</button></view......
  • C++ - 使用using定义别名
    大家都知道,在 C++ 中可以通过typedef重定义一个类型:typedefunsignedintuint_t;被重定义的类型并不是一个新的类型,仅仅只是原有的类型取了一个新的名字。因此,下面这样将不是合法的函数重载:voidfunc(unsignedint);voidfunc(uint_t); //error:redefinition使用ty......
  • C++ - 右值引用
    《C++11是什么》一节中提到,在C++98/03标准的基础上,C++11标准对C++语言增添了约140个新特性。本节要讲的右值引用就是众多新特性中的一个,同时也是最重要的特性之一。很多初学者都感觉右值引用晦涩难懂,其实不然。右值引用只不过是一种新的C++语法,真正理解起来有难度的是基......
  • C++ - 单例模式实现
    1.什么是单例模式单例模式是指在整个系统生命周期内,保证一个类只能产生一个实例,确保该类的唯一性。为什么需要单例模式两个原因:节省资源。一个类只有一个实例,不存在多份实例,节省资源。方便控制。在一些操作公共资源的场景时,避免了多个对象引起的复杂操作。但是在实现单例......
  • C++回调C#方法
    在VC中封装的网络通信模块,在异步接收到数据时需要将内容传递给C#中的消息处理函数,于是便出现了如标题所说的情况。   C++的回调函数中有一个参数,是处理接收到的字节流的回调函数指针,定义基本如下:   typedefvoid(*fpDataReceived)(char*data,intlen);......
  • C++ - 多线程之线程管理函数
    1.获取线程id函数get_id()的使用该函数在命名空间std::this_thread下。作用是获取当前线程的id。#include<iostream>#include<thread>usingnamespacestd;//No.1get_id()获取线程idvoidthreadFunc(){ cout<<"get_id()子线程id:"<<this_thread::get_id(......