鱼C论坛

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

[技术交流] C++旅程第12站——vector

[复制链接]
发表于 2020-6-15 22:14:57 | 显示全部楼层 |阅读模式

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

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

x
A dream that you donnot fight for can haunt you for the rest of your life.
                                                                         Vector
vector可以动态扩展(并不是在原空间之后续接新空间,而是找到更大的内存空间,然后将原数据拷贝到新空间,释放原空间)
vector容器的迭代器是支持随机访问的迭代器
  1. #include<iostream>
  2. #include<vector>

  3. using namespace std;

  4. void printvector(vector<int>&v)
  5. {
  6.         for (vector<int>::iterator it = v.begin();it != v.end(); it++)
  7.         {
  8.                 cout << *it << " ";
  9.         }
  10. }

  11. void test1()
  12. {
  13.        
  14.         vector<int>v1;//默认构造  无参构造
  15.        
  16.         for (int i = 0; i < 10; i++)
  17.         {
  18.                 v1.push_back(i);
  19.         }
  20.        
  21.         printvector(v1);
  22.         cout << endl;
  23.        
  24.         //通过区间方式进行构造
  25.         vector<int>v2(v1.begin(),v1.end());
  26.         printvector(v2);
  27.        
  28.         cout << endl;
  29.        
  30.         //n个elem方式构造
  31.         vector<int>v3(10,100);
  32.         printvector(v3);
  33.        
  34.         cout << endl;
  35.        
  36.         //拷贝构造
  37.         vector<int>v4(v3);
  38.         printvector(v4);
  39.        
  40.         cout << endl;
  41.          
  42. }

  43. void test2()
  44. {
  45.         vector<int>v1;
  46.        
  47.         for (int i = 0 ; i < 10;i++)
  48.         {
  49.                 v1.push_back(i);
  50.         }
  51.        
  52.         //赋值 operator=
  53.         vector<int>v2;
  54.         v2 = v1;
  55.         printvector(v2);
  56.         cout << endl;
  57.        
  58.         //assign
  59.         vector<int>v3;
  60.         v3.assign(v1.begin(),v1.end());
  61.         printvector(v3);
  62.         cout << endl;
  63.        
  64.         //n个elem方式赋值
  65.         vector<int>v4;
  66.         v4.assign(10,100);
  67.         printvector(v4);
  68.         cout << endl;
  69. }

  70. void test3()
  71. {
  72.         vector<int>v1;
  73.         for (int i = 0; i<10; i++)
  74.         {
  75.                 v1.push_back(i);
  76.         }
  77.        
  78.         if(v1.empty())
  79.         {
  80.                 cout << "v1为空" << endl;
  81.         }
  82.         else
  83.         {
  84.                 cout << "v1不为空" << endl;
  85.                 cout << "v1的容量为:" << v1.capacity() << endl;
  86.                 cout << "v1的大小为: " << v1.size() << endl;
  87.         }
  88.        
  89.         //重新指定大小
  90.         v1.resize(15,100);//利用重载版本,可以指定默认填充值,参数  15->个数 100->值
  91.         printvector(v1);//如果重新指定的比原来的长,默认用0填充新的位置
  92.         cout << endl;
  93.        
  94.         v1.resize(3);
  95.         printvector(v1);//如果重新指定的比原来的短,超出的部分删除掉
  96.         cout << endl;
  97. }

  98. void test4()
  99. {
  100.         vector<int>v1;//尾插
  101.         v1.push_back(10);
  102.         v1.push_back(20);
  103.         v1.push_back(30);
  104.         v1.push_back(40);
  105.         v1.push_back(50);
  106.         printvector(v1);
  107.         cout << endl;
  108.        
  109.         //尾删
  110.         v1.pop_back();
  111.         printvector(v1);
  112.         cout << endl;
  113.        
  114.         //插入
  115.         v1.insert(v1.begin(),2,1000);
  116.         printvector(v1);
  117.         cout << endl;
  118.        
  119.         //删除  参数也是迭代器
  120.         v1.erase(v1.begin());
  121.         printvector(v1);
  122.         cout << endl;
  123.        
  124.         //清空
  125.         v1.erase(v1.begin(),v1.end());
  126.         v1.clear();
  127.         printvector(v1);
  128.         cout << endl;
  129. }

  130. void test5()
  131. {
  132.         vector<int>v1;
  133.         for(int i = 0;i < 10 ;i++)
  134.         {
  135.                 v1.push_back(i);
  136.         }
  137.        
  138.         //利用[]方式访问数组中的元素
  139.         for (int i = 0;i < v1.size(); i++)
  140.         {
  141.                 cout << v1[i] << " ";
  142.         }
  143.         cout << endl;
  144.        
  145.         //利用at方式访问元素
  146.         for(int i = 0;i < v1.size(); i++)
  147.         {
  148.                 cout << v1.at(i) << " ";
  149.         }
  150.         cout << endl;
  151.        
  152.         //获取第一个元素
  153.         cout << "第一个元素为:" << v1.front() << endl;
  154.        
  155.         //获取最后一个元素
  156.         cout << "最后一个元素为: " << v1.back() << endl;
  157. }

  158. void test6()
  159. {
  160.         vector<int>v1;
  161.         for(int i = 0;i < 10 ;i++)
  162.         {
  163.                 v1.push_back(i);
  164.         }
  165.         printvector(v1);
  166.         cout << endl;
  167.        
  168.         vector<int>v2;
  169.         for(int i = 10;i > 0;i--)
  170.         {
  171.                 v2.push_back(i);
  172.         }
  173.         printvector(v2);
  174.         cout << endl;
  175.        
  176.         //交换
  177.         cout << "交换后!!!!!!!!!!" << endl;
  178.         v1.swap(v2);
  179.         printvector(v1);
  180.         cout << endl;
  181.         printvector(v2);
  182.         cout << endl;
  183.        
  184.         vector<int>v3;
  185.         for(int i = 0;i < 10000;i++)
  186.         {
  187.                 v3.push_back(i);
  188.         }
  189.         cout << "v3的容量: " << v3.capacity() << endl;
  190.         cout << "v3的大小: " << v3.size() << endl;
  191.        
  192.         //重新指定大小
  193.         v3.resize(5);
  194.         cout << "v3的容量: " << v3.capacity() << endl;
  195.         cout << "v3的大小: " << v3.size() << endl;
  196.        
  197.         //使用swap收缩空间
  198.         vector<int>(v3).swap(v3);
  199.        
  200.         cout << "v3的容量为: " << v3.capacity() << endl;
  201.         cout << "v3的大小为:  " << v3.size() << endl;
  202. }

  203. void test7()
  204. {
  205.         vector<int>v1;
  206.     v1.reserve(100000);//预留空间
  207.         int num = 0;
  208.         int *p = NULL;
  209.         for(int i = 0; i< 100000;i++)
  210.         {
  211.                 v1.push_back(i);
  212.                 if(p != &v1[0])
  213.                 {
  214.                         p = &v1[0];
  215.                         num++;
  216.                 }
  217.         }
  218.         cout << "num : " << num << endl;
  219. }

  220. int main()
  221. {
  222.         test1();//vector构造函数
  223.        
  224.         test2();//vector赋值操作
  225.        
  226.         test3();//vector容量和大小
  227.        
  228.         test4();//vector插入和删除
  229.        
  230.         test5();//vector数据存取
  231.        
  232.         test6();//vector互换
  233.        
  234.         test7();//vector预留空间
  235.        
  236.         return 0;
  237. }
复制代码

                                                                            噜啦啦

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-16 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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