鱼C论坛

 找回密码
 立即注册
查看: 1603|回复: 0

[技术交流] 万行代码计划 Day06 C++ Vector容器

[复制链接]
发表于 2021-7-31 23:32:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
万行代码计划
Day06,50行
进度350/10000
有兴趣的伙伴可以一起来,互相监督

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include <vector>
  4. #include <list>
  5. using namespace std;

  6. //
  7. void test01()
  8. {
  9.         vector<int> v;
  10.         for (int i = 0; i < 10; i++){
  11.                 v.push_back(i);
  12.                 cout << v.capacity() << endl;  // v.capacity()容器的容量大概是成倍增加(不一定是成倍),如果不足的话
  13.         }
  14. }

  15. /*
  16. vector构造函数
  17. vector<T> v; //采用模板实现类实现,默认构造函数
  18. vector(v.begin(), v.end());//将v[begin(), end())区间中的元素拷贝给本身。
  19. vector(n, elem);//构造函数将n个elem拷贝给本身。
  20. vector(const vector &vec);//拷贝构造函数。

  21. //例子 使用第二个构造函数 我们可以...
  22. int arr[] = {2,3,4,1,9};
  23. vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));

  24. 3.2.4.2 vector常用赋值操作
  25. assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
  26. assign(n, elem);//将n个elem拷贝赋值给本身。
  27. vector& operator=(const vector  &vec);//重载等号操作符
  28. swap(vec);// 将vec与本身的元素互换。

  29. 3.2.4.3 vector大小操作
  30. size();//返回容器中元素的个数
  31. empty();//判断容器是否为空
  32. resize(int num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
  33. resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长>度的元素被删除。
  34. capacity();//容器的容量
  35. reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。

  36. */

  37. void printVector( vector<int>&v)
  38. {
  39.         for (vector<int>::iterator it = v.begin(); it != v.end();it++)
  40.         {
  41.                 cout << *it << " ";
  42.         }
  43.         cout << endl;
  44. }

  45. void test02()
  46. {
  47.         vector <int >v;
  48.         int arr[] = { 2, 3, 4, 1, 9 };
  49.         vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));

  50.         vector<int>v2(v1.begin(), v1.end());

  51.         printVector(v2);

  52.         vector<int>v3(10, 100);

  53.         printVector(v3);

  54.         //赋值使用
  55.         vector<int>v4;
  56.         v4.assign(v3.begin(), v3.end());
  57.         printVector(v4);


  58.         v4.swap(v2);

  59.         cout << "after exchange v4 " << endl;
  60.         printVector(v4);

  61.         cout << "v4 size" << v4.size() << endl;

  62.         if (v4.empty())
  63.         {
  64.                 cout << "v4 is empty" << endl;
  65.         }
  66.         else
  67.         {
  68.                 cout << "v4 is not empty" << endl;
  69.         }
  70.        
  71.         //v4 23419
  72.         v4.resize(10,-1); //第二个参数是默认值 ,默认0;添加新元素并赋值;
  73.         printVector(v4);
  74.     cout << "v4 size" << v4.size() << endl;
  75.     cout << "v4 capacity" << v4.capacity() << endl;

  76.     v4.reserve(15);
  77.     cout << "v4 size" << v4.size() << endl;//size
  78.     cout << "v4 capacity" << v4.capacity() << endl;
  79.         cout << "v4[12]" << v4[12] << endl;

  80.         v4.resize(3);//变小的话,size改变,但capacity不变
  81.         printVector(v4);
  82.     cout << "v4 size" << v4.size() << endl;
  83.     cout << "v4 capacity" << v4.capacity() << endl;
  84.     cout << "v4[14]" << v4[14] << endl;

  85. }

  86. void test06()
  87. {
  88.         //逆序遍历
  89.         vector<int>v;
  90.         for ( int  i = 0; i < 10; i++)
  91.         {
  92.                 v.push_back(i);
  93.         }

  94.         //reverse_iterator
  95.         for (vector<int>::reverse_iterator it = v.rbegin(); it != v.rend();it++)
  96.         {
  97.                 cout << *it << " ";
  98.         }
  99.         cout << endl;

  100.         //vector迭代器是随机访问的迭代器  支持跳跃式访问
  101.         vector<int>::iterator itBegin = v.begin();
  102.         itBegin += 3;//
  103.         //如果上述写法不报错,这个迭代器是随机访问迭代器

  104.         list<int>l;
  105.         for (int i = 0; i < 10;i++)
  106.         {
  107.                 l.push_back(i);
  108.         }
  109.         list<int>::iterator lIt = l.begin();
  110.         //lIt += 1; //不支持随机访问
  111. }

  112. int main()
  113. {
  114.     test01();
  115.     test02();
  116.         test06();
  117. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 05:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表