bilibiliC++38-44_STL常用容器_deque容器

迈不过友情╰ 2022-10-30 02:28 257阅读 0赞

3.3 deque容器

3.3.1 deque容器基本概念

功能:

  • 双端数组,可以对头端进行插入删除操作

deque与vector区别:

  • vector对于头部的插入删除效率低,数据量越大,效率越低
  • deque相对而言,对头部的插入删除速度回比vector快
  • vector访问元素时的速度会比deque快,这和两者内部实现有关

在这里插入图片描述

deque内部工作原理:

deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据

中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间

在这里插入图片描述

  • deque容器的迭代器也是支持随机访问的

3.3.2 deque构造函数

功能描述:

  • deque容器构造

函数原型:

  • deque<T> deqT; //默认构造形式
  • deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身。
  • deque(n, elem); //构造函数将n个elem拷贝给本身。
  • deque(const deque &deq); //拷贝构造函数

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <deque>
  5. //const 防止修改数据
  6. void printDeque(const deque<int>& d)
  7. {
  8. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  9. cout << *it << " ";
  10. }
  11. cout << endl;
  12. }
  13. //deque构造
  14. void test01() {
  15. deque<int> d1; //无参构造函数
  16. for (int i = 0; i < 10; i++)
  17. {
  18. d1.push_back(i);
  19. }
  20. printDeque(d1);
  21. deque<int> d2(d1.begin(), d1.end());
  22. printDeque(d2);
  23. deque<int>d3(10, 100);
  24. printDeque(d3);
  25. deque<int>d4 = d3;
  26. printDeque(d4);
  27. }
  28. int main() {
  29. test01();
  30. system("pause");
  31. return 0;
  32. }
  33. /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 请按任意键继续. . . */

总结: deque容器和vector容器的构造方式几乎一致,灵活使用即可

3.3.3 deque赋值操作

功能描述:

  • 给deque容器进行赋值

函数原型:

  • deque& operator=(const deque &deq); //重载等号操作符
  • assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); //将n个elem拷贝赋值给本身。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <deque>
  5. void printDeque(const deque<int>& d)
  6. {
  7. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  8. cout << *it << " ";
  9. }
  10. cout << endl;
  11. }
  12. //赋值操作
  13. void test01()
  14. {
  15. deque<int> d1;
  16. for (int i = 0; i < 10; i++)
  17. {
  18. d1.push_back(i);
  19. }
  20. printDeque(d1);
  21. deque<int>d2;
  22. d2 = d1;
  23. printDeque(d2);
  24. deque<int>d3;
  25. d3.assign(d1.begin(), d1.end());
  26. printDeque(d3);
  27. deque<int>d4;
  28. d4.assign(10, 100);
  29. printDeque(d4);
  30. }
  31. int main() {
  32. test01();
  33. system("pause");
  34. return 0;
  35. }
  36. /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 请按任意键继续. . . */

总结:deque赋值操作也与vector相同,需熟练掌握

3.3.4 deque大小操作

功能描述:

  • 对deque容器的大小进行操作

函数原型:

  • deque.empty(); //判断容器是否为空
  • deque.size(); //返回容器中元素的个数
  • deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除。

  • deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <deque>
  4. void printDeque(const deque<int>& d)
  5. {
  6. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. //大小操作
  12. void test01()
  13. {
  14. deque<int> d1;
  15. for (int i = 0; i < 10; i++)
  16. {
  17. d1.push_back(i);
  18. }
  19. printDeque(d1);
  20. //判断容器是否为空
  21. if (d1.empty()) {
  22. cout << "d1为空!" << endl;
  23. }
  24. else {
  25. cout << "d1不为空!" << endl;
  26. //统计大小
  27. cout << "d1的大小为:" << d1.size() << endl;
  28. }
  29. //重新指定大小
  30. d1.resize(15, 1);
  31. printDeque(d1);
  32. d1.resize(5);
  33. printDeque(d1);
  34. }
  35. int main() {
  36. test01();
  37. system("pause");
  38. return 0;
  39. }

总结:

  • deque没有容量的概念
  • 判断是否为空 — empty
  • 返回元素个数 — size
  • 重新指定个数 — resize

3.3.5 deque 插入和删除

功能描述:

  • 向deque容器中插入和删除数据

函数原型:

两端插入操作:

  • push_back(elem); //在容器尾部添加一个数据
  • push_front(elem); //在容器头部插入一个数据
  • pop_back(); //删除容器最后一个数据
  • pop_front(); //删除容器第一个数据

指定位置操作:

  • insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
  • insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
  • insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
  • clear(); //清空容器的所有数据
  • erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
  • erase(pos); //删除pos位置的数据,返回下一个数据的位置。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <deque>
  4. void printDeque(const deque<int>& d)
  5. {
  6. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. //两端操作
  12. void test01()
  13. {
  14. deque<int> d;
  15. //尾插
  16. d.push_back(10);
  17. d.push_back(20);
  18. //头插
  19. d.push_front(100);
  20. d.push_front(200);
  21. printDeque(d);
  22. //尾删
  23. d.pop_back();
  24. //头删
  25. d.pop_front();
  26. printDeque(d);
  27. }
  28. //插入
  29. void test02()
  30. {
  31. deque<int> d;
  32. d.push_back(10);
  33. d.push_back(20);
  34. d.push_front(100);
  35. d.push_front(200);
  36. printDeque(d);
  37. d.insert(d.begin(), 1000);
  38. printDeque(d);
  39. d.insert(d.begin(), 2,10000);
  40. printDeque(d);
  41. deque<int>d2;
  42. d2.push_back(1);
  43. d2.push_back(2);
  44. d2.push_back(3);
  45. d.insert(d.begin(), d2.begin(), d2.end());
  46. printDeque(d);
  47. }
  48. //删除
  49. void test03()
  50. {
  51. deque<int> d;
  52. d.push_back(10);
  53. d.push_back(20);
  54. d.push_front(100);
  55. d.push_front(200);
  56. printDeque(d);
  57. d.erase(d.begin());
  58. printDeque(d);
  59. d.erase(d.begin(), d.end());
  60. d.clear();
  61. printDeque(d);
  62. }
  63. int main() {
  64. //test01();
  65. //test02();
  66. test03();
  67. system("pause");
  68. return 0;
  69. }

总结:

  • 插入和删除提供的位置是迭代器!
  • 尾插 — push_back
  • 尾删 — pop_back
  • 头插 — push_front
  • 头删 — pop_front

3.3.6 deque 数据存取

功能描述:

  • 对deque 中的数据的存取操作

函数原型:

  • at(int idx); //返回索引idx所指的数据
  • operator[]; //返回索引idx所指的数据
  • front(); //返回容器中第一个数据元素
  • back(); //返回容器中最后一个数据元素

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <deque>
  4. void printDeque(const deque<int>& d)
  5. {
  6. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. //数据存取
  12. void test01()
  13. {
  14. deque<int> d;
  15. d.push_back(10);
  16. d.push_back(20);
  17. d.push_front(100);
  18. d.push_front(200);
  19. for (int i = 0; i < d.size(); i++) {
  20. cout << d[i] << " ";
  21. }
  22. cout << endl;
  23. for (int i = 0; i < d.size(); i++) {
  24. cout << d.at(i) << " ";
  25. }
  26. cout << endl;
  27. cout << "front:" << d.front() << endl;
  28. cout << "back:" << d.back() << endl;
  29. }
  30. int main() {
  31. test01();
  32. system("pause");
  33. return 0;
  34. }

总结:

  • 除了用迭代器获取deque容器中元素,[ ]和at也可以
  • front返回容器第一个元素
  • back返回容器最后一个元素

3.3.7 deque 排序

功能描述:

  • 利用算法实现对deque容器进行排序

算法:

  • sort(iterator beg, iterator end) //对beg和end区间内元素进行排序

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <deque>
  4. #include <algorithm>
  5. void printDeque(const deque<int>& d)
  6. {
  7. for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
  8. cout << *it << " ";
  9. }
  10. cout << endl;
  11. }
  12. void test01()
  13. {
  14. deque<int> d;
  15. d.push_back(10);
  16. d.push_back(20);
  17. d.push_front(100);
  18. d.push_front(200);
  19. printDeque(d);
  20. sort(d.begin(), d.end());
  21. printDeque(d);
  22. }
  23. int main() {
  24. test01();
  25. system("pause");
  26. return 0;
  27. }

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

3.4 案例-评委打分

3.4.1 案例描述

有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。

3.4.2 实现步骤

  1. 创建五名选手,放到vector中
  2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
  3. sort算法对deque容器中分数排序,去除最高和最低分
  4. deque容器遍历一遍,累加总分
  5. 获取平均分

示例代码:

  1. #include<iostream>
  2. using namespace std;
  3. #include<vector>
  4. #include<deque>
  5. #include<algorithm>
  6. //选手类
  7. class Person
  8. {
  9. public:
  10. Person(string name, int score)
  11. {
  12. this->m_Name = name;
  13. this->m_Score = score;
  14. }
  15. string m_Name; //姓名
  16. int m_Score; //平均分
  17. };
  18. void createPerson(vector<Person>& v)
  19. {
  20. string nameSeed = "ABCDE";
  21. for (int i = 0; i < 5; i++)
  22. {
  23. string name = "选手";
  24. name += nameSeed[i];
  25. int score = 0;
  26. Person p(name, score);
  27. //将创建的person对象 放入到容器中
  28. v.push_back(p);
  29. }
  30. }
  31. //打分
  32. void setScore(vector<Person>& v)
  33. {
  34. for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
  35. {
  36. //将评委的分数 放入到deque容器中
  37. deque<int>d;
  38. for (int i = 0; i < 10; i++)
  39. {
  40. int score = rand() % 41 + 60; // 60 ~ 100
  41. d.push_back(score);
  42. }
  43. //cout << "选手: " << it->m_Name << " 打分: " << endl;
  44. //for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
  45. //{
  46. // cout << *dit << " ";
  47. //}
  48. //cout << endl;
  49. //排序
  50. sort(d.begin(), d.end());
  51. //去除最高和最低分
  52. d.pop_back();
  53. d.pop_front();
  54. //取平均分
  55. int sum = 0;
  56. for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
  57. {
  58. sum += *dit; //累加每个评委的分数
  59. }
  60. int avg = sum / d.size();
  61. //将平均分 赋值给选手身上
  62. it->m_Score = avg;
  63. }
  64. }
  65. void showScore(vector<Person>& v)
  66. {
  67. for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
  68. {
  69. cout << "姓名: " << it->m_Name << " 平均分: " << it->m_Score << endl;
  70. }
  71. }
  72. int main() {
  73. //随机数种子
  74. srand((unsigned int)time(NULL));
  75. //1、创建5名选手
  76. vector<Person>v; //存放选手容器
  77. createPerson(v);
  78. //测试
  79. //for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
  80. //{
  81. // cout << "姓名: " << (*it).m_Name << " 分数: " << (*it).m_Score << endl;
  82. //}
  83. //2、给5名选手打分
  84. setScore(v);
  85. //3、显示最后得分
  86. showScore(v);
  87. system("pause");
  88. return 0;
  89. }

总结: 选取不同的容器操作数据,可以提升代码的效率

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读