• CPP 核心编程9-STL


    STL初识

    2.1 STL的诞生

    • 长久以来,软件界一直希望建立一种可重复利用的东西
    • C++的面向对象和泛型编程思想,目的就是复用性的提升
    • 大多情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作
    • 为了建立数据结构和算法的一套标准,诞生了STL

    2.2 STL基本概念

    • STL(Standard Template Library,标准模板库)
    • STL 从广义上分为: 容器(container) 算法(algorithm) 迭代器(iterator)
    • 容器和算法之间通过迭代器进行无缝连接。
    • STL 几乎所有的代码都采用了模板类或者模板函数

    2.3 STL六大组件

    STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器

    1. 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
    2. 算法:各种常用的算法,如sort、find、copy、for_each等
    3. 迭代器:扮演了容器与算法之间的胶合剂。
    4. 仿函数:行为类似函数,可作为算法的某种策略。
    5. 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
    6. 空间配置器:负责空间的配置与管理。

    2.4 STL中容器、算法、迭代器

    **容器:**置物之所也

    STL容器就是将运用最广泛的一些数据结构实现出来

    常用的数据结构:数组, 链表,树, 栈, 队列, 集合, 映射表 等

    这些容器分为序列式容器和关联式容器两种:

    ​ 序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置。 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系

    **算法:**问题之解法也

    有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)

    算法分为:质变算法和非质变算法。

    质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等

    非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等

    **迭代器:**容器和算法之间粘合剂

    提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。

    每个容器都有自己专属的迭代器

    迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针

    迭代器种类:

    种类

    功能

    支持运算

    输入迭代器

    对数据的只读访问

    只读,支持++、==、!=

    输出迭代器

    对数据的只写访问

    只写,支持++

    前向迭代器

    读写操作,并能向前推进迭代器

    读写,支持++、==、!=

    双向迭代器

    读写操作,并能向前和向后操作

    读写,支持++、--,

    随机访问迭代器

    读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器

    读写,支持++、--、[n]、-n、、>=

    常用的容器中迭代器种类为双向迭代器,和随机访问迭代器

    2.5 容器算法迭代器初识

    了解STL中容器、算法、迭代器概念之后,我们利用代码感受STL的魅力

    STL中最常用的容器为Vector,可以理解为数组,下面我们将学习如何向这个容器中插入数据、并遍历这个容器

    2.5.1 vector存放内置数据类型

    容器: vector

    算法: for_each

    迭代器: vector::iterator

    1. #include "iostream"
    2. #include "vector"
    3. #include "algorithm"
    4. using namespace std;
    5. void myPrint(int val)
    6. {
    7. cout << val << endl;
    8. }
    9. void test()
    10. {
    11. //创建vector容器对象 并且通过模板参数指定容器中存放的数据类型
    12. vector<int> v;
    13. //向容器中放数据
    14. v.push_back(1);
    15. v.push_back(2);
    16. v.push_back(3);
    17. v.push_back(4);
    18. v.push_back(5);
    19. //每一个容器都有自己的迭代器,迭代器是用来遍历容器中的元素
    20. // v.begin()返回迭代器,这个迭代器指向容器中第一个数据
    21. // v.end()返回迭代器,这个迭代器指向容器元素的最后一个元素的下一个位置
    22. // vector::iterator 拿到vector这种容器的迭代器类型
    23. vector<int>::iterator iterBegin = v.begin();
    24. vector<int>::iterator iterEnd = v.end();
    25. //第一种遍历
    26. while (iterBegin != iterEnd)
    27. {
    28. cout << *iterBegin << endl;
    29. iterBegin++;
    30. }
    31. cout << endl;
    32. //第二种遍历
    33. for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    34. {
    35. cout << *it << endl;
    36. }
    37. cout << endl;
    38. //第三种遍历
    39. //使用STL提供的遍历算法 头文件 algorithm
    40. for_each(v.begin(), v.end(), myPrint);
    41. }
    42. int main()
    43. {
    44. test();
    45. return 0;
    46. }

    互换容器

    1. #include "iostream"
    2. #include "vector"
    3. using namespace std;
    4. //vector容器互换
    5. void printVector(vector<int> v) {
    6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    7. cout << *it << " ";
    8. }
    9. cout << endl;
    10. }
    11. //1 基本使用
    12. void test1() {
    13. vector<int> v1;
    14. for (int i = 0; i < 10; i++) {
    15. v1.push_back(i);
    16. }
    17. cout << "交换前:" << endl;
    18. printVector(v1);
    19. vector<int> v2;
    20. for (int j = 10; j > 0; j--) {
    21. v2.push_back(j);
    22. }
    23. printVector(v2);
    24. cout << "交换后:" << endl;
    25. v1.swap(v2);
    26. printVector(v1);
    27. printVector(v2);
    28. }
    29. //2 实际用途:巧用swap可以收缩内存空间
    30. void test2() {
    31. vector<int> v;
    32. for (int i = 0; i < 100000; i++) {
    33. v.push_back(i);
    34. }
    35. cout << "v的容量为:" << v.capacity() << endl;
    36. cout << "v的大小为:" << v.size() << endl;
    37. v.resize(3);
    38. cout << "resize后 v的容量为:" << v.capacity() << endl;
    39. cout << "resize后 v的大小为:" << v.size() << endl;
    40. //巧用swap收缩内存空间 后面有图片解释
    41. vector<int>(v).swap(v);
    42. cout << "swap后 v的容量为:" << v.capacity() << endl;
    43. cout << "swap后 v的大小为:" << v.size() << endl;
    44. }
    45. int main() {
    46. test1();
    47. test2();
    48. return 0;
    49. }
    50. 交换前:
    51. 0 1 2 3 4 5 6 7 8 9
    52. 10 9 8 7 6 5 4 3 2 1
    53. 交换后:
    54. 10 9 8 7 6 5 4 3 2 1
    55. 0 1 2 3 4 5 6 7 8 9
    56. v的容量为:131072
    57. v的大小为:100000
    58. resize后 v的容量为:131072
    59. resize后 v的大小为:3
    60. swap后 v的容量为:3
    61. swap后 v的大小为:3

    总结:swap可以使两个容器互换,可以达到实用的收缩内存效果

     

     

    上图解释:

    vector(v) 利用拷贝构造函数创建了一个匿名对象,新的对象没有名,x代表这个匿名对象,会按照v当前size来初始化匿名对象,所以匿名对象一开始size=3,capacity=3,只不过这是匿名对象。

    上面的匿名对象,系统会帮助我们回收,匿名对象有一个特点,当前行执行完,编译器发现它是匿名对象,马上回收。

    1. #include "iostream"
    2. #include "vector"
    3. using namespace std;
    4. //vector 容器 预留空间
    5. void test() {
    6. vector<int> v;
    7. int num = 0;//统计开辟次数
    8. int *p = nullptr;
    9. // for (int i = 0; i < 100000; i++) {
    10. // v.push_back(i);
    11. // if (p != &v[0]) {
    12. // p = &v[0];
    13. // num++;
    14. // }
    15. // }
    16. int capacity = 0;
    17. for (int i = 0; i < 100000; i++) {
    18. v.push_back(i);
    19. if (v.capacity() != capacity) {
    20. capacity = v.capacity();
    21. num++;
    22. }
    23. }
    24. cout << "不用reserve时,动态扩展次数:" << num << endl;
    25. int num2 = 0;
    26. int capacity2 = 0;
    27. vector<int> v2;
    28. v2.reserve(100000);
    29. for (int j = 0; j < 100000; j++) {
    30. v2.push_back(j);
    31. if (v2.capacity() != capacity) {
    32. capacity = v2.capacity();
    33. num2++;
    34. }
    35. }
    36. cout << "使用reserve时,动态扩展次数:" << num2 << endl;
    37. }
    38. int main() {
    39. test();
    40. return 0;
    41. }
    42. 不用reserve时,动态扩展次数:18
    43. 使用reserve时,动态扩展次数:1
    1. #include "iostream"
    2. #include "deque"
    3. #include "vector"
    4. #include "algorithm"
    5. using namespace std;
    6. void printDeque(deque<int> &d) {
    7. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. }
    12. //deque容器排序
    13. void test() {
    14. deque<int> d;
    15. d.push_back(10);
    16. d.push_back(20);
    17. d.push_back(30);
    18. d.push_front(100);
    19. d.push_front(200);
    20. d.push_front(300);
    21. cout << "排序前:" << endl;
    22. printDeque(d);
    23. //默认从小到大 升序
    24. //对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序
    25. //vector容器也可以利用 sort进行排序
    26. sort(d.begin(), d.end());
    27. cout << "排序后:" << endl;
    28. printDeque(d);
    29. }
    30. void test2() {
    31. vector<int> v;
    32. v.push_back(134);
    33. v.push_back(54);
    34. v.push_back(1432);
    35. v.push_back(43);
    36. v.push_back(1);
    37. sort(v.begin(), v.end());
    38. cout << "vector 排序后:" << endl;
    39. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    40. cout << *it << " ";
    41. }
    42. cout << endl;
    43. }
    44. int main() {
    45. test();
    46. test2();
    47. return 0;
    48. }
    49. 排序前:
    50. 300 200 100 10 20 30
    51. 排序后:
    52. 10 20 30 100 200 300
    53. vector 排序后:
    54. 1 43 54 134 1432

    总结:sort算法非常实用,使用时包含头文件algorithm

    1. #include "iostream"
    2. #include "vector"
    3. #include "deque"
    4. #include "algorithm"
    5. using namespace std;
    6. class Person {
    7. public:
    8. Person(string name, int score) {
    9. this->m_Name = name;
    10. this->m_Score = score;
    11. }
    12. int m_Score;
    13. string m_Name;
    14. };
    15. void createPerson(vector &vp) {
    16. string nameSeed = "ABCDE";
    17. for (int i = 0; i < 5; i++) {
    18. Person p("选手" + to_string(i + 1), 0);
    19. vp.push_back(p);
    20. }
    21. }
    22. void printInfo(vector &p) {
    23. for (vector::iterator it = p.begin(); it != p.end(); it++) {
    24. cout << "姓名:" << (*it).m_Name << " 平均分:" << (*it).m_Score << endl;
    25. }
    26. }
    27. void setScore(vector &p) {
    28. srand((unsigned int) time(NULL));
    29. for (vector::iterator it = p.begin(); it != p.end(); it++) {
    30. deque<int> d;
    31. cout << "选手:" << it->m_Name << "的打分:" << endl;
    32. for (int i = 0; i < 10; i++) {
    33. int score = rand() % 41 + 60;
    34. d.push_back(score);
    35. cout << score << " ";
    36. }
    37. cout << endl;
    38. //排序
    39. sort(d.begin(), d.end());
    40. //去除最高和最低
    41. d.pop_back();
    42. d.pop_front();
    43. int sum = 0;
    44. for (deque<int>::const_iterator dit = d.begin(); dit != d.end(); dit++) {
    45. sum += *dit;
    46. }
    47. it->m_Score = sum / d.size();
    48. }
    49. }
    50. //评委打分
    51. void test() {
    52. //1创建5名选手
    53. vector v;
    54. createPerson(v);
    55. printInfo(v);
    56. //2 给5名选手打分
    57. setScore(v);
    58. printInfo(v);
    59. //3 显示最后得分
    60. }
    61. int main() {
    62. test();
    63. return 0;
    64. }
    65. 姓名:选手1 平均分:0
    66. 姓名:选手2 平均分:0
    67. 姓名:选手3 平均分:0
    68. 姓名:选手4 平均分:0
    69. 姓名:选手5 平均分:0
    70. 选手:选手1的打分:
    71. 83 96 87 61 93 65 87 77 71 73
    72. 选手:选手2的打分:
    73. 83 70 77 97 63 88 79 62 70 70
    74. 选手:选手3的打分:
    75. 94 100 64 68 83 96 81 92 85 73
    76. 选手:选手4的打分:
    77. 65 78 89 62 73 67 71 96 90 62
    78. 选手:选手5的打分:
    79. 63 78 93 99 84 81 83 88 62 91
    80. 姓名:选手1 平均分:79
    81. 姓名:选手2 平均分:75
    82. 姓名:选手3 平均分:84
    83. 姓名:选手4 平均分:74
    84. 姓名:选手5 平均分:82

     

    1. #include "iostream"
    2. #include "stack"
    3. using namespace std;
    4. //栈
    5. //特点,先进后出
    6. void test() {
    7. stack<int> s;
    8. s.push(1);
    9. s.push(2);
    10. s.push(3);
    11. s.push(4);
    12. cout << "栈的大小:" << s.size() << endl;
    13. while (!s.empty()) {
    14. cout << s.top() << endl;
    15. s.pop();
    16. }
    17. cout << "栈的大小:" << s.size() << endl;
    18. }
    19. int main() {
    20. test();
    21. return 0;
    22. }
    1. #include "iostream"
    2. #include "queue"
    3. using namespace std;
    4. //queue容器
    5. void test() {
    6. queue<int> q;
    7. q.push(1);
    8. q.push(2);
    9. q.push(3);
    10. q.push(4);
    11. while (!q.empty()) {
    12. cout << "front=" << q.front() << " back=" << q.back() << " q.size() =" << q.size() << endl;
    13. q.pop();
    14. }
    15. }
    16. int main() {
    17. test();
    18. return 0;
    19. }
    20. front=1 back=4 q.size() =4
    21. front=2 back=4 q.size() =3
    22. front=3 back=4 q.size() =2
    23. front=4 back=4 q.size() =1

     

     

    1. #include "iostream"
    2. #include "list"
    3. using namespace std;
    4. void printList(const list<int> &l) {
    5. for (auto i:l) {
    6. cout << i << " ";
    7. }
    8. cout << endl;
    9. }
    10. //list容器构造函数
    11. void test() {
    12. list<int> l1;
    13. l1.push_back(1);
    14. l1.push_back(2);
    15. l1.push_back(3);
    16. l1.push_back(4);
    17. printList(l1);
    18. //区间方式构造
    19. list<int> l2(l1.begin(), l1.end());
    20. printList(l2);
    21. //拷贝构造
    22. list<int> l3(l2);
    23. printList(l3);
    24. //n个element
    25. list<int> l4(10, 1);
    26. printList(l4);
    27. }
    28. int main() {
    29. test();
    30. return 0;
    31. }
    32. 1 2 3 4
    33. 1 2 3 4
    34. 1 2 3 4
    35. 1 1 1 1 1 1 1 1 1 1
    1. #include "iostream"
    2. #include "list"
    3. using namespace std;
    4. void printList(const list<int> &l) {
    5. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
    6. cout << *it << " ";
    7. }
    8. cout << endl;
    9. }
    10. //list赋值和交换
    11. void test() {
    12. list<int> l1;
    13. l1.push_back(1);
    14. l1.push_back(2);
    15. l1.push_back(3);
    16. l1.push_back(4);
    17. printList(l1);
    18. list<int> l2 = l1;
    19. printList(l2);
    20. list<int> l3;
    21. l3.assign(l2.begin(), l2.end());
    22. printList(l3);
    23. list<int> l4;
    24. l4.assign(10, 1);
    25. printList(l4);
    26. }
    27. //交换
    28. void test2() {
    29. list<int> l1;
    30. l1.push_back(1);
    31. l1.push_back(2);
    32. l1.push_back(3);
    33. l1.push_back(4);
    34. list<int> l2;
    35. l2.assign(10, 1);
    36. cout << "交换前:" << endl;
    37. printList(l1);
    38. printList(l2);
    39. l1.swap(l2);
    40. cout << "交换后:" << endl;
    41. printList(l1);
    42. printList(l2);
    43. };
    44. int main() {
    45. test();
    46. cout << endl;
    47. test2();
    48. return 0;
    49. }
    50. 1 2 3 4
    51. 1 2 3 4
    52. 1 2 3 4
    53. 1 1 1 1 1 1 1 1 1 1
    54. 交换前:
    55. 1 2 3 4
    56. 1 1 1 1 1 1 1 1 1 1
    57. 交换后:
    58. 1 1 1 1 1 1 1 1 1 1
    59. 1 2 3 4
    1. #include "iostream"
    2. #include "list"
    3. using namespace std;
    4. void printList(const list<int> &l) {
    5. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
    6. cout << *it << " ";
    7. }
    8. cout << endl;
    9. }
    10. //list 大小操作
    11. void test() {
    12. list<int> l1;
    13. l1.push_back(1);
    14. l1.push_back(2);
    15. l1.push_back(3);
    16. l1.push_back(4);
    17. printList(l1);
    18. if (l1.empty()) {
    19. cout << "l1为空" << endl;
    20. } else {
    21. cout << "l1不为空" << endl;
    22. cout << "l1个数:" << l1.size() << endl;
    23. }
    24. //重新指定大小
    25. l1.resize(2);
    26. printList(l1);
    27. l1.resize(4, 100);
    28. printList(l1);
    29. }
    30. int main() {
    31. test();
    32. return 0;
    33. }
    34. 1 2 3 4
    35. l1不为空
    36. l1个数:4
    37. 1 2
    38. 1 2 100 100
    1. #include "iostream"
    2. #include "list"
    3. using namespace std;
    4. void printList(list<int> &l) {
    5. for (auto i:l) {
    6. cout << i << " ";
    7. }
    8. cout << endl;
    9. }
    10. //list 插入和删除
    11. void test() {
    12. list<int> l1;
    13. //尾插
    14. l1.push_back(1);
    15. l1.push_back(2);
    16. l1.push_back(3);
    17. l1.push_back(4);
    18. //头插
    19. l1.push_front(100);
    20. l1.push_front(200);
    21. l1.push_front(300);
    22. printList(l1);
    23. //尾删
    24. l1.pop_back();
    25. //头删
    26. l1.pop_front();
    27. printList(l1);
    28. // 200 100 1 2 3
    29. l1.insert(l1.begin(), 1000);
    30. // 1000 200 100 1 2 3
    31. printList(l1);
    32. list<int>::iterator it = l1.begin();
    33. l1.insert(++it, 2000);
    34. // 1000 2000 200 100 1 2 3
    35. printList(l1);
    36. it = l1.begin();
    37. //删除
    38. l1.erase(it);
    39. // 2000 200 100 1 2 3
    40. printList(l1);
    41. l1.erase(++it);
    42. // 200 100 1 2 3
    43. printList(l1);
    44. it = l1.begin();
    45. l1.erase(++it);
    46. // 200 1 2 3
    47. printList(l1);
    48. //移除
    49. l1.push_back(200);
    50. l1.push_back(1);
    51. // 200 1 2 3 200 1
    52. printList(l1);
    53. l1.remove(1);
    54. // 200 2 3 200
    55. printList(l1);
    56. //clear
    57. l1.clear();
    58. printList(l1);
    59. }
    60. int main() {
    61. test();
    62. return 0;
    63. }
    64. 300 200 100 1 2 3 4
    65. 200 100 1 2 3
    66. 1000 200 100 1 2 3
    67. 1000 2000 200 100 1 2 3
    68. 2000 200 100 1 2 3
    69. 200 100 1 2 3
    70. 200 1 2 3
    71. 200 1 2 3 200 1
    72. 200 2 3 200

    list 反转和排序

    **功能描述:**

    * 将容器中的元素反转,以及将容器中的数据进行排序

    **函数原型:**

    * `reverse();` //反转链表

    * `sort();` //链表排序

    1. #include "iostream"
    2. using namespace std;
    3. #include "list"
    4. #include "algorithm"
    5. void printList(const list<int> &l)
    6. {
    7. for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    8. {
    9. cout << *it << " ";
    10. }
    11. cout << endl;
    12. }
    13. bool myCompare(int a, int b)
    14. {
    15. return a > b;
    16. }
    17. void test()
    18. {
    19. list<int> l;
    20. l.push_back(1);
    21. l.push_back(2);
    22. l.push_back(3);
    23. l.push_back(4);
    24. printList(l);
    25. //反转
    26. l.reverse();
    27. printList(l);
    28. //排序
    29. // sort(l.begin(), l.end()); list不支持随机访问,不能用sort,得用内置的排序算法
    30. l.sort(); // 1 2 3 4 默认从小到大排序
    31. printList(l);
    32. l.sort(myCompare); //指定规则,从大到小
    33. printList(l);
    34. }
    35. int main()
    36. {
    37. test();
    38. return 0;
    39. }

    总结:

    * 反转 --- reverse

    * 排序 --- sort (成员函数)

    3.7.8 排序案例

    案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高

    排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

    1. #include "iostream"
    2. #include "list"
    3. using namespace std;
    4. //排序案例
    5. class Person
    6. {
    7. public:
    8. Person(string name, int age, int height)
    9. {
    10. mage = age;
    11. mname = name;
    12. mheight = height;
    13. }
    14. string mname;
    15. int mage;
    16. int mheight;
    17. };
    18. void printList(const list &l)
    19. {
    20. for (list::const_iterator it = l.begin(); it != l.end(); it++)
    21. {
    22. cout << it->mname << " " << it->mage << " " << it->mheight << endl;
    23. }
    24. }
    25. bool myCompare(const Person &p1, const Person &p2)
    26. {
    27. if (p1.mage == p2.mage)
    28. {
    29. return p1.mheight > p2.mheight;
    30. }
    31. return p1.mage < p2.mage;
    32. }
    33. void test()
    34. {
    35. Person p1("a", 33, 178);
    36. Person p2("b", 45, 172);
    37. Person p3("c", 22, 183);
    38. Person p4("d", 22, 156);
    39. Person p5("e", 22, 188);
    40. list l;
    41. l.push_back(p1);
    42. l.push_back(p2);
    43. l.push_back(p3);
    44. l.push_back(p4);
    45. l.push_back(p5);
    46. cout << "before sort:" << endl;
    47. printList(l);
    48. l.sort(myCompare);
    49. cout << "after sort:" << endl;
    50. printList(l);
    51. }
    52. int main()
    53. {
    54. test();
    55. return 0;
    56. }
    57. before sort:
    58. a 33 178
    59. b 45 172
    60. c 22 183
    61. d 22 156
    62. e 22 188
    63. after sort:
    64. e 22 188
    65. c 22 183
    66. d 22 156
    67. a 33 178
    68. b 45 172

    总结:

    • 对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序
    • 高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂

    3.8 set/ multiset 容器

    3.8.1 set基本概念

    简介:

    • 所有元素都会在插入时自动被排序

    本质:

    • set/multiset属于关联式容器,底层结构是用二叉树实现。

    set和multiset区别:

    • set不允许容器中有重复的元素
    • multiset允许容器中有重复的元素

    3.8.2 set构造和赋值

    功能描述:创建set容器以及赋值

    构造:

    • set st; //默认构造函数:
    • set(const set &st); //拷贝构造函数

    赋值:

    • set& operator=(const set &st); //重载等号操作符
    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. // set
    5. void printSet(const set<int> s)
    6. {
    7. for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    8. {
    9. cout << *it << " ";
    10. }
    11. cout << endl;
    12. }
    13. void test()
    14. {
    15. set<int> s;
    16. s.insert(1);
    17. s.insert(3);
    18. s.insert(43);
    19. s.insert(33);
    20. s.insert(2);
    21. printSet(s); //排好序的 默认是从小打到
    22. set<int> s2(s);
    23. printSet(s2);
    24. set<int> s3;
    25. s3 = s2;
    26. printSet(s3);
    27. }
    28. int main()
    29. {
    30. test();
    31. return 0;
    32. }

    总结:

    • set容器插入数据时用insert
    • set容器插入数据的数据会自动排序

    3.8.3 set大小和交换

    功能描述:

    • 统计set容器大小以及交换set容器

    函数原型:

    • size(); //返回容器中元素的数目
    • empty(); //判断容器是否为空
    • swap(st); //交换两个集合容器
    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. // set
    5. void printSet(const set<int> s)
    6. {
    7. for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    8. {
    9. cout << *it << " ";
    10. }
    11. cout << endl;
    12. }
    13. void test()
    14. {
    15. set<int> s;
    16. s.insert(1);
    17. s.insert(3);
    18. s.insert(43);
    19. s.insert(33);
    20. s.insert(2);
    21. printSet(s); //排好序的 默认是从小打到
    22. set<int> s2(s);
    23. printSet(s2);
    24. set<int> s3;
    25. s3 = s2;
    26. printSet(s3);
    27. }
    28. //大小
    29. void test2()
    30. {
    31. set<int> s1;
    32. s1.insert(1);
    33. s1.insert(3);
    34. s1.insert(2);
    35. s1.insert(4);
    36. if (s1.empty())
    37. {
    38. cout << "empty" << endl;
    39. }
    40. else
    41. {
    42. cout << "not empty,size is " << s1.size() << endl;
    43. }
    44. }
    45. //交换
    46. void test3()
    47. {
    48. set<int> s1;
    49. s1.insert(1);
    50. s1.insert(3);
    51. s1.insert(2);
    52. s1.insert(4);
    53. set<int> s2;
    54. s2.insert(100);
    55. s2.insert(300);
    56. s2.insert(200);
    57. s2.insert(400);
    58. cout << "before swap" << endl;
    59. printSet(s1);
    60. printSet(s2);
    61. cout << endl;
    62. cout << "after swap" << endl;
    63. s1.swap(s2);
    64. printSet(s1);
    65. printSet(s2);
    66. }
    67. int main()
    68. {
    69. test();
    70. test2();
    71. test3();
    72. return 0;
    73. }

    总结:

    • 统计大小 --- size
    • 判断是否为空 --- empty
    • 交换容器 --- swap

    3.8.4 set插入和删除

    功能描述:

    • set容器进行插入数据和删除数据

    函数原型:

    • insert(elem); //在容器中插入元素。
    • clear(); //清除所有元素
    • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    • erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    • erase(elem); //删除容器中值为elem的元素。
    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. // set
    5. void printSet(const set<int> s)
    6. {
    7. for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    8. {
    9. cout << *it << " ";
    10. }
    11. cout << endl;
    12. }
    13. void test()
    14. {
    15. set<int> s1;
    16. //插入
    17. s1.insert(10);
    18. s1.insert(30);
    19. s1.insert(20);
    20. s1.insert(40);
    21. printSet(s1);
    22. //删除
    23. s1.erase(s1.begin());
    24. printSet(s1);
    25. s1.erase(30);
    26. printSet(s1);
    27. //清空
    28. // s1.erase(s1.begin(), s1.end());//等同下面
    29. s1.clear();
    30. printSet(s1);
    31. }
    32. int main()
    33. {
    34. test();
    35. return 0;
    36. }

    总结:

    • 插入 --- insert
    • 删除 --- erase
    • 清空 --- clear

    3.8.5 set查找和统计

    功能描述:

    • 对set容器进行查找数据以及统计数据

    函数原型:

    • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    • count(key); //统计key的元素个数
    1. #include "iostream"
    2. using namespace std;
    3. #include "set"
    4. void test()
    5. {
    6. set<int> s;
    7. s.insert(1);
    8. s.insert(2);
    9. s.insert(3);
    10. s.insert(4);
    11. //查找
    12. set<int>::iterator it = s.find(3);
    13. if (it != s.end())
    14. {
    15. cout << "found" << endl;
    16. }
    17. else
    18. {
    19. cout << "not found" << endl;
    20. }
    21. //统计
    22. int num = s.count(2);
    23. cout << "num:" << num << endl;
    24. }
    25. int main()
    26. {
    27. test();
    28. return 0;
    29. }

    总结:

    • 查找 --- find (返回的是迭代器)
    • 统计 --- count (对于set,结果为0或者1)

    3.8.6 set和multiset区别

    学习目标:

    • 掌握set和multiset的区别

    区别:

    • set不可以插入重复数据,而multiset可以
    • set插入数据的同时会返回插入结果,表示插入是否成功
    • multiset不会检测数据,因此可以插入重复数据
    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. // set 和 multiset 区别
    5. void test()
    6. {
    7. set<int> s;
    8. pairint>::iterator, bool> ret = s.insert(10);
    9. if (ret.second)
    10. {
    11. cout << "fisrt insert ok" << endl;
    12. }
    13. else
    14. {
    15. cout << "fisrt insert not ok" << endl;
    16. }
    17. ret = s.insert(10);
    18. if (ret.second)
    19. {
    20. cout << "second insert ok" << endl;
    21. }
    22. else
    23. {
    24. cout << "second insert not ok" << endl;
    25. }
    26. // multiset
    27. multiset<int> ms;
    28. ms.insert(1);
    29. ms.insert(2);
    30. ms.insert(1);
    31. for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
    32. {
    33. cout << *it << " ";
    34. }
    35. cout << endl;
    36. }
    37. int main()
    38. {
    39. test();
    40. return 0;
    41. }
    42. fisrt insert ok
    43. second insert not ok
    44. 1 1 2

    总结:

    • 如果不允许插入重复数据可以利用set
    • 如果需要插入重复数据利用multiset

    3.8.7 pair对组创建

    功能描述:

    • 成对出现的数据,利用对组可以返回两个数据

    两种创建方式:

    • pair p ( value1, value2 );
    • pair p = make_pair( value1, value2 );
    1. #include "iostream"
    2. using namespace std;
    3. //创建对组
    4. void test()
    5. {
    6. pairint> p("aa", 1);
    7. cout << p.first << " " << p.second << endl;
    8. pair<int, char> p2 = make_pair(1, 'c');
    9. cout << p2.first << " " << p2.second << endl;
    10. }
    11. int main()
    12. {
    13. test();
    14. return 0;
    15. }
    16. aa 1
    17. 1 c

    总结:

    两种方式都可以创建对组,记住一种即可

    3.8.8 set容器排序

    学习目标:

    • set容器默认排序规则为从小到大,掌握如何改变排序规则

    主要技术点:

    • 利用仿函数,可以改变排序规则
    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. class MyCompare
    5. {
    6. public:
    7. bool operator()(int a, int b)
    8. {
    9. return a > b;
    10. }
    11. };
    12. void test()
    13. {
    14. set<int> s1;
    15. s1.insert(10);
    16. s1.insert(40);
    17. s1.insert(20);
    18. s1.insert(30);
    19. s1.insert(50);
    20. //默认从小到大
    21. for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
    22. {
    23. cout << *it << " ";
    24. }
    25. cout << endl;
    26. //指定排序规则
    27. set<int, MyCompare> s2;
    28. s2.insert(10);
    29. s2.insert(40);
    30. s2.insert(20);
    31. s2.insert(30);
    32. s2.insert(50);
    33. for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
    34. {
    35. cout << *it << " ";
    36. }
    37. cout << endl;
    38. }
    39. int main()
    40. {
    41. test();
    42. return 0;
    43. }
    44. 10 20 30 40 50
    45. 50 40 30 20 10

    总结:利用仿函数可以指定set容器的排序规则

    示例二 set存放自定义数据类型

    1. #include "iostream"
    2. #include "set"
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. Person(string name, int age)
    8. {
    9. m_Name = name;
    10. m_Age = age;
    11. }
    12. string m_Name;
    13. int m_Age;
    14. };
    15. class comparePerson
    16. {
    17. public:
    18. bool operator()(const Person &p1, const Person &p2)
    19. {
    20. return p1.m_Age > p2.m_Age;
    21. }
    22. };
    23. void test()
    24. {
    25. set s;
    26. Person p1("fad", 23);
    27. Person p2("sgdf", 27);
    28. Person p3("hgf", 25);
    29. Person p4("grwe", 21);
    30. s.insert(p1);
    31. s.insert(p2);
    32. s.insert(p3);
    33. s.insert(p4);
    34. for (set::iterator it = s.begin(); it != s.end(); it++)
    35. {
    36. cout << "name: " << it->m_Name << " age: " << it->m_Age << endl;
    37. }
    38. }
    39. int main()
    40. {
    41. test();
    42. return 0;
    43. }
    44. name: sgdf age: 27
    45. name: hgf age: 25
    46. name: fad age: 23
    47. name: grwe age: 21

    总结:

    对于自定义数据类型,set必须指定排序规则才可以插入数据

    3.9 map/ multimap容器

    3.9.1 map基本概念

    简介:

    • map中所有元素都是pair
    • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
    • 所有元素都会根据元素的键值自动排序

    本质:

    • map/multimap属于关联式容器,底层结构是用二叉树实现。

    优点:

    • 可以根据key值快速找到value值

    map和multimap区别:

    • map不允许容器中有重复key值元素
    • multimap允许容器中有重复key值元素

    3.9.2 map构造和赋值

    功能描述:

    • 对map容器进行构造和赋值操作

    函数原型:

    构造:

    • map mp; //map默认构造函数:
    • map(const map &mp); //拷贝构造函数

    赋值:

    • map& operator=(const map &mp); //重载等号操作符
  • 相关阅读:
    算法复杂度分析中的渐近分析(基于输入大小)
    四向车立体库|四向穿梭车AGV如何进行入库和出库?
    java计算机毕业设计网上拍卖系统MyBatis+系统+LW文档+源码+调试部署
    京东二面:高并发设计,都有哪些技术方案?
    蓝桥杯官网填空题(矩形切割)
    欢度中秋节!从零开始实现一个月饼检测器
    docke安装mysql以及主从搭建(并且指定数据生成路径)
    Vuex④(多组件共享数据、Vuex模块化+namespace)
    2、适配器模式
    水电站与数据可视化:洞察未来能源趋势的窗口
  • 原文地址:https://blog.csdn.net/wade1010/article/details/128210539