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.初始化示例
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
- class A  
- {  
-     
- };  
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<int> vecInt;  
-   
-     
-     vector<float> vecFloat;  
-   
-     
-     vector<A> vecA;  
-   
-     
-     vector<A*> vecPointA;  
-   
-     return 0;  
- }  
 
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
- class A  
- {  
-     
- };  
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<int> vecIntA(3);  
-       
-     
-     vector<int> vecIntB(3,9);  
-   
-     
-     vector<int> vecIntC(vecIntB);  
-       
-     int iArray[]={2,4,6};  
-     
-     vector<int> vecIntD(iArray,iArray+3);  
-   
-     
-     
-   
-     cout<<"vecIntA:"<<endl;  
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     cout<<"VecIntB:"<<endl;  
-     for(vector<int>::iterator it = vecIntB.begin() ;it!=vecIntB.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     cout<<"VecIntB:"<<endl;  
-     for(vector<int>::iterator it = vecIntC.begin() ;it!=vecIntC.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     cout<<"vecIntD:"<<endl;  
-     for(vector<int>::iterator it = vecIntD.begin() ;it!=vecIntD.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-     return 0;  
- }  
 
程序的运行结果如下:
 

 
上面的代码用了4种方法建立vector并对其初始化
  2.增加及获得元素示例:
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<int> vecIntA;  
-   
-     
-     vecIntA.push_back(1);  
-     vecIntA.push_back(2);  
-     vecIntA.push_back(3);  
-       
-     int nSize = vecIntA.size();  
-   
-     cout<<"vecIntA:"<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecIntA[i]<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecIntA.at(i)<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-       
-     return 0;  
- }  
 
上述代码对一个整形向量类进行操作,先定义一个整形元素向量类,然后插入3个值,并用3种不同的方法输出,程序运行结果如下:
 

 
 
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
- class A  
- {  
- public:  
-     int n;  
- public:  
-     A(int n)  
-     {  
-         this->n = n;  
-     }  
- };  
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<A> vecClassA;  
-   
-     A a1(1);  
-     A a2(2);  
-     A a3(3);  
-   
-     
-     vecClassA.push_back(a1);  
-     vecClassA.push_back(a2);  
-     vecClassA.push_back(a3);  
-       
-       
-     int nSize = vecClassA.size();  
-   
-     cout<<"vecClassA:"<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecClassA[i].n<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecClassA.at(i).n<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     for(vector<A>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)  
-     {  
-         cout<<(*it).n<<"     ";  
-     }  
-     cout<<endl;  
-       
-     return 0;  
- }  
 
上述代码通过定义元素为类的向量,通过插入3个初始化的类,并通过3种方法输出,运行结果如下:
 

 
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
- class A  
- {  
- public:  
-     int n;  
- public:  
-     A(int n)  
-     {  
-         this->n = n;  
-     }  
- };  
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<A*> vecClassA;  
-   
-     A *a1 = new A(1);  
-     A *a2 = new A(2);  
-     A *a3 = new A(3);  
-   
-     
-     vecClassA.push_back(a1);  
-     vecClassA.push_back(a2);  
-     vecClassA.push_back(a3);  
-       
-       
-     int nSize = vecClassA.size();  
-   
-     cout<<"vecClassA:"<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecClassA[i]->n<<"\t";  
-     }  
-     cout<<endl;  
-   
-     
-     for(int i=0;i<nSize;i++)  
-     {  
-         cout<<vecClassA.at(i)->n<<"\t";  
-     }  
-     cout<<endl;  
-   
-     
-     for(vector<A*>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)  
-     {  
-         cout<<(**it).n<<"\t";  
-     }  
-     cout<<endl;  
-     delete a1; delete a2;delete a3;  
-     return 0;  
- }  
 
上述代码通过定义元素为类指针的向量,通过插入3个初始化的类指针,并通过3种方法输出指针指向的类,运行结果如下:
 

  3.修改元素示例
修改元素的方法主要有三种:1.通过数组修改,2.通过引用修改,3.通过迭代器修改
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<int> vecIntA;  
-   
-     
-     vecIntA.push_back(1);  
-     vecIntA.push_back(2);  
-     vecIntA.push_back(3);  
-       
-     int nSize = vecIntA.size();  
-   
-     
-     cout<<"通过数组修改,第二个元素为8:"<<endl;  
-     vecIntA[1]=8;  
-   
-     cout<<"vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-       
-     
-     cout<<"通过引用修改,第二个元素为18:"<<endl;  
-     int &m = vecIntA.at(1);  
-     m=18;  
-   
-     cout<<"vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-   
-     
-     cout<<"通过迭代器修改,第二个元素为28"<<endl;  
-     vector<int>::iterator itr = vecIntA.begin()+1;  
-     *itr = 28;  
-   
-     cout<<"vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"     ";  
-     }  
-     cout<<endl;  
-   
-     return 0;  
- }  
 
程序运行结果如下:
 
 
4.删除向量示例
删除向量主要通过erase和pop_back,示例代码如下
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
-   
- using namespace std;  
-   
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-       
-     
-     vector<int> vecIntA;  
-   
-     
-     for(int i=1;i<=10;i++)  
-     {  
-         vecIntA.push_back(i);  
-     }  
-       
-     vecIntA.erase(vecIntA.begin()+4);  
-           
-     cout<<"删除第5个元素后的向量vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"\t";  
-     }  
-     cout<<endl;  
-   
-     
-     vecIntA.erase(vecIntA.begin()+1,vecIntA.begin()+5);  
-   
-     cout<<"删除第2-5个元素后的vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"\t";  
-     }  
-     cout<<endl;  
-   
-     
-     vecIntA.pop_back();  
-   
-     cout<<"删除最后一个元素后的vecIntA:"<<endl;  
-     
-     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
-     {  
-         cout<<*it<<"\t";  
-     }  
-     cout<<endl;  
-   
-     return 0;  
- }  
 
程序运行结果如下:
 
 
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.综合示例
-   
- #include "stdafx.h"  
- #include<iostream>  
- #include<vector>  
- #include<string>  
- using namespace std;  
-   
- class Student  
- {  
- public:  
-     string m_strNO;  
-     string m_strName;  
-     string m_strSex;  
-     string m_strDate;  
- public:  
-     Student(string strNO,string strName,string strSex,string strDate)  
-     {  
-         m_strNO = strNO;  
-         m_strName = strName;  
-         m_strSex = strSex;  
-         m_strDate = strDate;  
-     }  
-     void Display()  
-     {  
-         cout<<m_strNO<<"\t";  
-         cout<<m_strName<<"\t";  
-         cout<<m_strSex<<"\t";  
-         cout<<m_strDate<<"\t";  
-     }  
- };  
-   
- class StudCollect  
- {  
-     vector<Student> m_vStud;  
- public:  
-     void Add(Student &s)  
-     {  
-         m_vStud.push_back(s);  
-     }  
-     Student* Find(string strNO)  
-     {  
-         bool bFind = false;  
-         int i;  
-         for(i = 0;i < m_vStud.size();i++)  
-         {  
-             Student& s = m_vStud.at(i);  
-             if(s.m_strNO == strNO)  
-             {  
-                 bFind = true;  
-                 break;  
-             }  
-         }  
-         Student *s = NULL;  
-         if(bFind)  
-             s = &m_vStud.at(i);  
-         return s;  
-     }  
- };  
-   
- int _tmain(int argc, _TCHAR* argv[])  
- {  
-     Student s1("1001","zhangsan","boy","1988-10-10");     
-     Student s2("1002","lisi","boy","1988-8-25");  
-     Student s3("1003","wangwu","boy","1989-2-14");  
-   
-     StudCollect s;  
-     s.Add(s1);  
-     s.Add(s2);  
-     s.Add(s3);  
-   
-     Student *ps = s.Find("1002");  
-     if(ps)  
-         ps->Display();  
-     return 0;  
- }  
 
代码运行实例如下:
 

vector容器用法详解
原文:http://www.cnblogs.com/zsq1993/p/5929806.html