vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组。像数组一样,vector类也用从0开始的下标表示元素的位置;但和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大和缩小而自动变化。

vector类常用的函数如下所示:

1.构造函数

  • vector():创建一个空vector
  • vector(int nSize):创建一个vector,元素个数为nSize
  • vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t
  • vector(const vector&):复制构造函数
  • vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中

2.增加函数

  • void push_back(const T& x):向量尾部增加一个元素X
  • iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
  • iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
  • iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据

3.删除函数

  • iterator erase(iterator it):删除向量中迭代器指向元素
  • iterator erase(iterator first,iterator last):删除向量中[first,last)中元素
  • void pop_back():删除向量中最后一个元素
  • void clear():清空向量中所有元素

4.遍历函数

  • reference at(int pos):返回pos位置元素的引用
  • reference front():返回首元素的引用
  • reference back():返回尾元素的引用
  • iterator begin():返回向量头指针,指向第一个元素
  • iterator end():返回向量尾指针,指向向量最后一个元素的下一个位置
  • reverse_iterator rbegin():反向迭代器,指向最后一个元素
  • reverse_iterator rend():反向迭代器,指向第一个元素之前的位置

5.判断函数

  • bool empty() const:判断向量是否为空,若为空,则向量中无元素

6.大小函数

  • int size() const:返回向量中元素的个数
  • int capacity() const:返回当前向量张红所能容纳的最大元素值
  • int max_size() const:返回最大可允许的vector元素数量值

7.其他函数

  • void swap(vector&):交换两个同类型向量的数据
  • void assign(int n,const T& x):设置向量中第n个元素的值为x
  • void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素

示例:

1.初始化示例

  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<vector>
  4. using namespace std;
  5. class A
  6. {
  7. //空类
  8. };
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. //int型vector
  12. vector<int> vecInt;
  13. //float型vector
  14. vector<float> vecFloat;
  15. //自定义类型,保存类A的vector
  16. vector<A> vecA;
  17. //自定义类型,保存指向类A的指针的vector
  18. vector<A*> vecPointA;
  19. return 0;
  20. }
  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. class A
  8. {
  9. //空类
  10. };
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13. //int型vector,包含3个元素
  14. vector<int> vecIntA(3);
  15. //int型vector,包含3个元素且每个元素都是9
  16. vector<int> vecIntB(3,9);
  17. //复制vecIntB到vecIntC
  18. vector<int> vecIntC(vecIntB);
  19. int iArray[]={2,4,6};
  20. //创建vecIntD
  21. vector<int> vecIntD(iArray,iArray+3);
  22. //打印vectorA,此处也可以用下面注释内的代码来输出vector中的数据
  23. /*for(int i=0;i<vecIntA.size();i++)
  24. {
  25. cout<<vecIntA[i]<<"     ";
  26. }*/
  27. cout<<"vecIntA:"<<endl;
  28. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  29. {
  30. cout<<*it<<"     ";
  31. }
  32. cout<<endl;
  33. //打印vecIntB
  34. cout<<"VecIntB:"<<endl;
  35. for(vector<int>::iterator it = vecIntB.begin() ;it!=vecIntB.end();it++)
  36. {
  37. cout<<*it<<"     ";
  38. }
  39. cout<<endl;
  40. //打印vecIntC
  41. cout<<"VecIntB:"<<endl;
  42. for(vector<int>::iterator it = vecIntC.begin() ;it!=vecIntC.end();it++)
  43. {
  44. cout<<*it<<"     ";
  45. }
  46. cout<<endl;
  47. //打印vecIntD
  48. cout<<"vecIntD:"<<endl;
  49. for(vector<int>::iterator it = vecIntD.begin() ;it!=vecIntD.end();it++)
  50. {
  51. cout<<*it<<"     ";
  52. }
  53. cout<<endl;
  54. return 0;
  55. }

程序的运行结果如下:

上面的代码用了4种方法建立vector并对其初始化

2.增加及获得元素示例:

  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. //int型vector,包含3个元素
  10. vector<int> vecIntA;
  11. //插入1 2 3
  12. vecIntA.push_back(1);
  13. vecIntA.push_back(2);
  14. vecIntA.push_back(3);
  15. int nSize = vecIntA.size();
  16. cout<<"vecIntA:"<<endl;
  17. //打印vectorA,方法一:
  18. for(int i=0;i<nSize;i++)
  19. {
  20. cout<<vecIntA[i]<<"     ";
  21. }
  22. cout<<endl;
  23. //打印vectorA,方法二:
  24. for(int i=0;i<nSize;i++)
  25. {
  26. cout<<vecIntA.at(i)<<"     ";
  27. }
  28. cout<<endl;
  29. //打印vectorA,方法三:
  30. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  31. {
  32. cout<<*it<<"     ";
  33. }
  34. cout<<endl;
  35. return 0;
  36. }

上述代码对一个整形向量类进行操作,先定义一个整形元素向量类,然后插入3个值,并用3种不同的方法输出,程序运行结果如下:

 
  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. class A
  8. {
  9. public:
  10. int n;
  11. public:
  12. A(int n)
  13. {
  14. this->n = n;
  15. }
  16. };
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. //int型vector,包含3个元素
  20. vector<A> vecClassA;
  21. A a1(1);
  22. A a2(2);
  23. A a3(3);
  24. //插入1 2 3
  25. vecClassA.push_back(a1);
  26. vecClassA.push_back(a2);
  27. vecClassA.push_back(a3);
  28. int nSize = vecClassA.size();
  29. cout<<"vecClassA:"<<endl;
  30. //打印vecClassA,方法一:
  31. for(int i=0;i<nSize;i++)
  32. {
  33. cout<<vecClassA[i].n<<"     ";
  34. }
  35. cout<<endl;
  36. //打印vecClassA,方法二:
  37. for(int i=0;i<nSize;i++)
  38. {
  39. cout<<vecClassA.at(i).n<<"     ";
  40. }
  41. cout<<endl;
  42. //打印vecClassA,方法三:
  43. for(vector<A>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
  44. {
  45. cout<<(*it).n<<"     ";
  46. }
  47. cout<<endl;
  48. return 0;
  49. }

上述代码通过定义元素为类的向量,通过插入3个初始化的类,并通过3种方法输出,运行结果如下:

  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. class A
  8. {
  9. public:
  10. int n;
  11. public:
  12. A(int n)
  13. {
  14. this->n = n;
  15. }
  16. };
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. //int型vector,包含3个元素
  20. vector<A*> vecClassA;
  21. A *a1 = new A(1);
  22. A *a2 = new A(2);
  23. A *a3 = new A(3);
  24. //插入1 2 3
  25. vecClassA.push_back(a1);
  26. vecClassA.push_back(a2);
  27. vecClassA.push_back(a3);
  28. int nSize = vecClassA.size();
  29. cout<<"vecClassA:"<<endl;
  30. //打印vecClassA,方法一:
  31. for(int i=0;i<nSize;i++)
  32. {
  33. cout<<vecClassA[i]->n<<"\t";
  34. }
  35. cout<<endl;
  36. //打印vecClassA,方法二:
  37. for(int i=0;i<nSize;i++)
  38. {
  39. cout<<vecClassA.at(i)->n<<"\t";
  40. }
  41. cout<<endl;
  42. //打印vecClassA,方法三:
  43. for(vector<A*>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
  44. {
  45. cout<<(**it).n<<"\t";
  46. }
  47. cout<<endl;
  48. delete a1; delete a2;delete a3;
  49. return 0;
  50. }

上述代码通过定义元素为类指针的向量,通过插入3个初始化的类指针,并通过3种方法输出指针指向的类,运行结果如下:

3.修改元素示例

修改元素的方法主要有三种:1.通过数组修改,2.通过引用修改,3.通过迭代器修改
  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. //int型vector,包含3个元素
  10. vector<int> vecIntA;
  11. //插入1 2 3
  12. vecIntA.push_back(1);
  13. vecIntA.push_back(2);
  14. vecIntA.push_back(3);
  15. int nSize = vecIntA.size();
  16. //通过引用修改vector
  17. cout<<"通过数组修改,第二个元素为8:"<<endl;
  18. vecIntA[1]=8;
  19. cout<<"vecIntA:"<<endl;
  20. //打印vectorA
  21. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  22. {
  23. cout<<*it<<"     ";
  24. }
  25. cout<<endl;
  26. //通过引用修改vector
  27. cout<<"通过引用修改,第二个元素为18:"<<endl;
  28. int &m = vecIntA.at(1);
  29. m=18;
  30. cout<<"vecIntA:"<<endl;
  31. //打印vectorA
  32. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  33. {
  34. cout<<*it<<"     ";
  35. }
  36. cout<<endl;
  37. //通过迭代器修改vector
  38. cout<<"通过迭代器修改,第二个元素为28"<<endl;
  39. vector<int>::iterator itr = vecIntA.begin()+1;
  40. *itr = 28;
  41. cout<<"vecIntA:"<<endl;
  42. //打印vectorA
  43. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  44. {
  45. cout<<*it<<"     ";
  46. }
  47. cout<<endl;
  48. return 0;
  49. }

程序运行结果如下:

 

4.删除向量示例

删除向量主要通过erase和pop_back,示例代码如下
  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. //int型vector,包含3个元素
  10. vector<int> vecIntA;
  11. //循环插入1 到10
  12. for(int i=1;i<=10;i++)
  13. {
  14. vecIntA.push_back(i);
  15. }
  16. vecIntA.erase(vecIntA.begin()+4);
  17. cout<<"删除第5个元素后的向量vecIntA:"<<endl;
  18. //打印vectorA
  19. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  20. {
  21. cout<<*it<<"\t";
  22. }
  23. cout<<endl;
  24. //删除第2-5个元素
  25. vecIntA.erase(vecIntA.begin()+1,vecIntA.begin()+5);
  26. cout<<"删除第2-5个元素后的vecIntA:"<<endl;
  27. //打印vectorA
  28. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  29. {
  30. cout<<*it<<"\t";
  31. }
  32. cout<<endl;
  33. //删除最后一个元素
  34. vecIntA.pop_back();
  35. cout<<"删除最后一个元素后的vecIntA:"<<endl;
  36. //打印vectorA
  37. for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
  38. {
  39. cout<<*it<<"\t";
  40. }
  41. cout<<endl;
  42. return 0;
  43. }

程序运行结果如下:

 

3.进一步理解vector,如下图所示:

    当执行代码vector<int> v(2,5)时,在内存里建立了2个整形元素空间,值是5.当增加一个元素时,原有的空间由2个编程4个整形元素空间,并把元素1放入第3个整形空间,第4个空间作为预留空间。当增加元素2时,直接把值2放入第4个空间。当增加元素3时,由于原有向量中没有预留空间,则内存空间由4个变为8个整形空间,并把值放入第5个内存空间。
   总之,扩大新元素时,如果超过当前的容量,则容量会自动扩充2倍,如果2倍容量仍不足,则继续扩大2倍。本图是直接在原空间基础上画的新增空间,其实要复杂的多,包括重新配置、元素移动、释放原始空间的过程。因此对vector容器而言,当增加新的元素时,有可能很快完成(直接存在预留空间中),有可能稍慢(扩容后再放新元素);对修改元素值而言是较快的;对删除元素来说,弱删除尾部元素较快,非尾部元素稍慢,因为牵涉到删除后的元素移动。

4.综合示例

  1. // vectorsample.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<vector>
  6. #include<string>
  7. using namespace std;
  8. class Student
  9. {
  10. public:
  11. string m_strNO;
  12. string m_strName;
  13. string m_strSex;
  14. string m_strDate;
  15. public:
  16. Student(string strNO,string strName,string strSex,string strDate)
  17. {
  18. m_strNO = strNO;
  19. m_strName = strName;
  20. m_strSex = strSex;
  21. m_strDate = strDate;
  22. }
  23. void Display()
  24. {
  25. cout<<m_strNO<<"\t";
  26. cout<<m_strName<<"\t";
  27. cout<<m_strSex<<"\t";
  28. cout<<m_strDate<<"\t";
  29. }
  30. };
  31. class StudCollect
  32. {
  33. vector<Student> m_vStud;
  34. public:
  35. void Add(Student &s)
  36. {
  37. m_vStud.push_back(s);
  38. }
  39. Student* Find(string strNO)
  40. {
  41. bool bFind = false;
  42. int i;
  43. for(i = 0;i < m_vStud.size();i++)
  44. {
  45. Student& s = m_vStud.at(i);
  46. if(s.m_strNO == strNO)
  47. {
  48. bFind = true;
  49. break;
  50. }
  51. }
  52. Student *s = NULL;
  53. if(bFind)
  54. s = &m_vStud.at(i);
  55. return s;
  56. }
  57. };
  58. int _tmain(int argc, _TCHAR* argv[])
  59. {
  60. Student s1("1001","zhangsan","boy","1988-10-10");
  61. Student s2("1002","lisi","boy","1988-8-25");
  62. Student s3("1003","wangwu","boy","1989-2-14");
  63. StudCollect s;
  64. s.Add(s1);
  65. s.Add(s2);
  66. s.Add(s3);
  67. Student *ps = s.Find("1002");
  68. if(ps)
  69. ps->Display();
  70. return 0;
  71. }

代码运行实例如下:

vector容器用法详解的更多相关文章

  1. STL:vector容器用法详解

    vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...

  2. STL之二:vector容器用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8507394 vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组. ...

  3. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  4. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  5. C++:vector的用法详解

    原文地址:http://blog.csdn.net/hancunai0017/article/details/7032383 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于 ...

  6. C++标准模板库(STL)——vector常见用法详解

    vector的定义 vector<typename> name; 相当于定义了一个一维数组name[SIZE],只不过其长度可以根据需要进行变化,比较节省空间,通俗来讲,vector就是& ...

  7. 7-set用法详解

    C++中set用法详解 转载 http://blog.csdn.net/yas12345678/article/details/52601454 C++ / set 更详细见:http://www.c ...

  8. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  9. STL set 常见用法详解

    <算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...

随机推荐

  1. Java遍历Map对象的方式

    public static void main(String[] args) { HashMap<String, String> testMap = new HashMap<> ...

  2. php调用API支付接口 可个人使用,无需营业执照(使用第三方接口,调用的天工接口。)

    首先访问  https://charging.teegon.com/  注册账号, 找到开发配置   记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击  S ...

  3. 20165312 2017-2018-2 《JAVA程序设计》第4周学习总结

    一.课本五六章知识点总结 1.第五章 继承是一种由已有的类创建新类的机制 子类继承父类的成员变量和方法 子类继承的方法只能操作子类继承和隐藏的成员变量 子类重写或新增的方法只能操作子类继承和新声明的成 ...

  4. ps-如何去水印

    现在,版权意识越来越明显了,所以加水印的图片越来越多了,但我们在一些特定的情况又不得不去使用那些图片,去水印又是问题.今天,我来说下如何去水印. 一.ps-仿制图章工具去水印 1.打开ps,打开待去水 ...

  5. Java笔试面试题整理第五波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  6. 笔记本 原来win10系统改装win7系统遇到 invaid signature detected.check secure boot policy setup问题

    这次操作的笔记本电脑是   华硕R414U 大家如果遇到类似问题的话也可以参考这个方法,但是必须搞清楚电脑的型号,型号不同操作起来有差别的 我这里选择的重装系统的方法是最简单粗暴的硬盘安装方法,怎么硬 ...

  7. javascript常用工具类整理(copy)

    JavaScript常用工具类 类型 日期 数组 字符串 数字 网络请求 节点 存储 其他 1.类型 isString (o) { //是否字符串 return Object.prototype.to ...

  8. hadoop的hdfs中的javaAPI操作

    package cn.itcast.bigdata.hdfs; import java.net.URI; import java.util.Iterator; import java.util.Map ...

  9. 合批只是对CPU的优化,与GPU没有任何关系

    如题. 今天细想了下合批这个东西. 合批是节省了CPU的相关准备工作的工作量. 合批后,经过VS,PS,尝试测试,模板测试后,此时已没有了纹理,顶点,索引的概念,只剩下一个个孤立的像素,各像素间没有任 ...

  10. C# 反编译

    今儿也是运气背,不知怎么的,一脚就把电脑踢关机了(其实就轻轻碰到了一下主机),我去,写了一早上的代码,尼玛就不见,不见就算了,其实是保存了的,主要是文件还损坏了,尼玛,那心情!!! 然后就想着恢复,下 ...