序列型容器


其中的元素都是可排序的(ordered),STL提供了vector,list,deque等序列式容器,而stack,queue,priority_queue则是容器适配器

数组

概念

  • 代表内存里一组连续的同类型存储区
  • 可以用来把多个存储区合并成一个整体

比如int arr[10] = {1,2,3,4,5,6};
image.png
image.png
image.png
差一错误(off-by-one error)

image.png
image.png
image.png
下面的Index应为0

image.png
image.png
image.png

数组的访问 数组的访问效率比较高
image.png
image.png

数组长度的获取

len = sizeof(a)/sizeof(a[0]); //获取数组长度

二维数组
image.png

image.png

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. int main()
  5. {
  6. // 数组访问
  7. //int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 0, 0};
  8. // 推荐方式
  9. //for (int index = 0; index < 10; ++index)
  10. // cout << a[index] << " ";
  11. //cout << endl;
  12. // 不推荐方式
  13. //for (int index = 0; index <= 9; ++index)
  14. // cout << a[index] << " ";
  15. // 数组的查找
  16. //int a[] = { 1, 2, 3, 4 };
  17. //int len = sizeof(a) / sizeof(a[0]);//得到数组容量
  18. //for (int index = 0; index < len; ++index)
  19. //{
  20. // if (a[index] == 5)
  21. // {
  22. // cout << index << endl;
  23. // return 0;
  24. // }
  25. //}
  26. //二维数组的访问:
  27. int a[2][4] = { { 1, 2, 3, 4 },{ 5,6,7,8 } };
  28. for (int row = 0; row < 2; ++row)
  29. {
  30. for (int col = 0; col < 4; ++col)
  31. {
  32. cout << a[row][col] << " ";
  33. }
  34. cout << endl;
  35. }
  36. return 0;
  37. }

vector

image.png
image.png
image.png
image.png
image.png

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6. // 创建动态数组vector
  7. vector<int> vec = { 1,2,3,4 };
  8. cout << "size is " << vec.size() << endl;
  9. cout << "capacity is " << vec.capacity() << endl;
  10. // 遍历所有元素
  11. for (int index = 0; index < vec.size(); ++index)
  12. {
  13. cout << vec[index] << endl;
  14. }
  15. // 在尾部插入一个元素5
  16. vec.push_back(5);
  17. cout << "size is " << vec.size() << endl;
  18. cout << "capacity is " << vec.capacity() << endl;
  19. // 遍历所有元素
  20. for (int index = 0; index < vec.size(); ++index)
  21. {
  22. cout << vec[index] << endl;
  23. }
  24. // 在中间插入一个元素6 查找的元素前面插入
  25. vec.insert(--vec.end(), 6);
  26. cout << "size is " << vec.size() << endl;
  27. cout << "capacity is " << vec.capacity() << endl;
  28. // 遍历所有元素
  29. for (int index = 0; index < vec.size(); ++index)
  30. {
  31. cout << vec[index] << endl;
  32. }
  33. // 在尾部移除一个元素
  34. vec.pop_back();
  35. cout << "size is " << vec.size() << endl;
  36. cout << "capacity is " << vec.capacity() << endl;
  37. // 遍历所有元素
  38. for (int index = 0; index < vec.size(); ++index)
  39. {
  40. cout << vec[index] << endl;
  41. }
  42. // 在任意位置移除一个元素
  43. vec.erase(vec.end() - 2);
  44. cout << "size is " << vec.size() << endl;
  45. cout << "capacity is " << vec.capacity() << endl;
  46. // 遍历所有元素
  47. for (int index = 0; index < vec.size(); ++index)
  48. {
  49. cout << vec[index] << endl;
  50. }
  51. return 0;
  52. }

vector是面向对象方式的动态数组,容量可以自动增长

使用最简单的数组,无法实现动态扩容插入元素,因为容量有限

插入

vec.insert(—vec.end(), 4);

删除

vec.pop_back()
vec.erase(vec.end()-1)

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include "mylog.h"
  5. #include "testVector.h"
  6. using namespace std;
  7. vector<string> allName;
  8. void test01(){
  9. allName.push_back("111");
  10. allName.push_back("222");
  11. allName.push_back("333");
  12. allName.clear();
  13. LOG("容器的内存大小是:%d", allName.capacity());
  14. // 替换的方式:来解决此问题
  15. vector<string> tempVector; // 定义临时容器目的:就是为了解决 、 替换全局容器,让全局容器不占用内存空间
  16. tempVector.swap(allName); // 把全局的 全部移动 给临时 == 把全局的给回收了
  17. LOG("容器的内存大小是:%d", allName.capacity());
  18. } // 函数一弹栈 tempVector给回收了,就保证了,全局和临时全部回收完毕
  19. void main_testVector(){
  20. // C++ 中容器,分为两种类型 vector栈的数据结构
  21. // 1.序列式容器:元素的排序关系,和元素本身没有任何关系,是我们在添加的时候导致的顺序导致的排序
  22. vector<int> vec01(1);
  23. vector<const char *> vec02(100, "肚子腾");
  24. vector<const char *> vec03;
  25. //添加元素
  26. vec03.push_back("A");
  27. vec03.push_back("B");
  28. vec03.push_back("C");
  29. //删除元素
  30. vec03.pop_back();
  31. //查
  32. const char * value1 = vec03.at(0);
  33. const char * value2 = vec03.at(1);
  34. LOG("第一个元素是:%s", value1);
  35. LOG("第二个元素是:%s", value2);
  36. // 遍历
  37. vector<const char *>::iterator beginResult = vec03.begin();
  38. vector<const char *>::iterator endResult = vec03.end();
  39. for(;beginResult != endResult; beginResult++){
  40. LOG("遍历到的元素是:%s", *(beginResult));
  41. }
  42. vec03.clear();
  43. LOG("此容器的内存空间是:%d", vec03.capacity());
  44. // test01();
  45. }

queue

  1. #include <iostream>
  2. #include <queue>
  3. #include "mylog.h"
  4. #include "testQueue.h"
  5. using namespace std;
  6. void main_testQueue(){
  7. queue<int> queue1;
  8. //增
  9. queue1.push(1);
  10. queue1.push(2);
  11. queue1.push(3);
  12. //删
  13. queue1.pop();
  14. int front = queue1.front();
  15. int back = queue1.back();
  16. int size = queue1.size();
  17. LOG("队头元素是:%d", front);
  18. LOG("队尾元素是:%d", back);
  19. LOG("队列元素个数是:%d", size);
  20. //队列不建议遍历
  21. }

stack栈


  1. #include <iostream>
  2. #include <stack>
  3. #include "mylog.h"
  4. #include "testStack.h"
  5. using namespace std;
  6. //栈先进后出
  7. void main_testStack(){
  8. stack<int> stack1;
  9. //增
  10. stack1.push(1);
  11. stack1.push(2);
  12. stack1.push(3);
  13. //删
  14. stack1.pop();
  15. //查
  16. int top = stack1.top();
  17. LOG("栈顶部元素是:%d", top);
  18. }

优先级队列priority_queue

  1. #include <iostream>
  2. #include <stack>
  3. #include "mylog.h"
  4. #include "testStack.h"
  5. using namespace std;
  6. //栈先进后出
  7. void main_testStack(){
  8. stack<int> stack1;
  9. //增
  10. stack1.push(1);
  11. stack1.push(2);
  12. stack1.push(3);
  13. //删
  14. stack1.pop();
  15. //查
  16. int top = stack1.top();
  17. LOG("栈顶部元素是:%d", top);
  18. }

关联式容器

通过一个关键字 来保存 和 访问 元素的,当元素被插入到容器时,按其建以某种特定规则放入适当位置 例如:Java中的 map set 都是关联式容器

map

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include "mylog.h"
  5. #include "testMap.h"
  6. using namespace std;
  7. void main_testMap() {
  8. map<int, char> map1;
  9. //增
  10. map1.insert(pair<int, char>(1, 'c'));
  11. map1.insert(pair<int, char>(2, 'g'));
  12. map1.insert(pair<int, char>(3, 'd'));
  13. //删
  14. map1.erase(1);
  15. //改
  16. map1.insert(pair<int, char>(1, 'e'));
  17. //查
  18. map<int, char>::iterator iterator2 = map1.find(1);
  19. LOG("第一个元素key=%d, value=%c", iterator2->first, iterator2->second);
  20. //遍历
  21. map<int, char>::iterator iteratorBegin = map1.begin();
  22. map<int, char>::iterator iteratorEnd = map1.end();
  23. for (; iteratorBegin != iteratorEnd; iteratorBegin++) {
  24. LOG("遍历的元素key=%d, value=%c", iteratorBegin->first, iteratorBegin->second);
  25. }
  26. }

set

  1. #pragma once;
  2. #include <iostream>
  3. #include <string>
  4. #include <set>
  5. #include <vector>
  6. #include "testSet.h"
  7. #include "mylog.h"
  8. using namespace std;
  9. void main_testSet() {
  10. set<int> set1 = {1, 2, 3, 4, 5};
  11. //增
  12. set1.insert(777);
  13. set1.insert(888);
  14. set1.insert(999);
  15. //改
  16. set1.insert(1); // 重复的元素添加不进去,因为set不允许添加重复的元素
  17. //删除元素
  18. set1.erase(1);
  19. //查
  20. set<int>::iterator iterator1 = set1.find(777);
  21. LOG("查找到的元素是:%d", *iterator1);
  22. //set遍历
  23. set<int>::iterator beginResult = set1.begin();
  24. set<int>::iterator endResult = set1.end();
  25. for (; beginResult != endResult; beginResult++) {
  26. LOG("查找的元素是%d", *beginResult);
  27. }
  28. }

迭代器失效问题
image.png
image.png
image.png
按名字删除
image.png
删除全部
image.png

迭代器

是一种smart pointer,用于访问顺序容器和关联容器中的元素,相当于容器和操纵容器的算法质检的中介

迭代器按照定义方式分为以下四种

1.正向迭代器: iterator
2.常量正向迭代器:const_iterator
3.反向迭代器:reverse_iterator
4.常量反向迭代器:const_reverse_iterator