vector容器用法詳解


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.初始化示例

[cpp]  view plain  copy
 
  1. #include "stdafx.h"  
  2. #include<iostream>  
  3. #include<vector>  
  4.   
  5. using namespace std;  
  6.   
  7. class A  
  8. {  
  9.     //空類  
  10. };  
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.       
  14.     //int型vector  
  15.     vector<int> vecInt;  
  16.   
  17.     //float型vector  
  18.     vector<float> vecFloat;  
  19.   
  20.     //自定義類型,保存類A的vector  
  21.     vector<A> vecA;  
  22.   
  23.     //自定義類型,保存指向類A的指針的vector  
  24.     vector<A*> vecPointA;  
  25.   
  26.     return 0;  
  27. }  

[cpp]  view plain  copy
 
  1. // vectorsample.cpp : 定義控制台應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include<iostream>  
  6. #include<vector>  
  7.   
  8. using namespace std;  
  9.   
  10. class A  
  11. {  
  12.     //空類  
  13. };  
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.       
  17.     //int型vector,包含3個元素  
  18.     vector<int> vecIntA(3);  
  19.       
  20.     //int型vector,包含3個元素且每個元素都是9  
  21.     vector<int> vecIntB(3,9);  
  22.   
  23.     //復制vecIntB到vecIntC  
  24.     vector<int> vecIntC(vecIntB);  
  25.       
  26.     int iArray[]={2,4,6};  
  27.     //創建vecIntD  
  28.     vector<int> vecIntD(iArray,iArray+3);  
  29.   
  30.     //打印vectorA,此處也可以用下面注釋內的代碼來輸出vector中的數據  
  31.     /*for(int i=0;i<vecIntA.size();i++) 
  32.     { 
  33.         cout<<vecIntA[i]<<"     "; 
  34.     }*/  
  35.   
  36.     cout<<"vecIntA:"<<endl;  
  37.     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
  38.     {  
  39.         cout<<*it<<"     ";  
  40.     }  
  41.     cout<<endl;  
  42.   
  43.     //打印vecIntB  
  44.     cout<<"VecIntB:"<<endl;  
  45.     for(vector<int>::iterator it = vecIntB.begin() ;it!=vecIntB.end();it++)  
  46.     {  
  47.         cout<<*it<<"     ";  
  48.     }  
  49.     cout<<endl;  
  50.   
  51.     //打印vecIntC  
  52.     cout<<"VecIntB:"<<endl;  
  53.     for(vector<int>::iterator it = vecIntC.begin() ;it!=vecIntC.end();it++)  
  54.     {  
  55.         cout<<*it<<"     ";  
  56.     }  
  57.     cout<<endl;  
  58.   
  59.     //打印vecIntD  
  60.     cout<<"vecIntD:"<<endl;  
  61.     for(vector<int>::iterator it = vecIntD.begin() ;it!=vecIntD.end();it++)  
  62.     {  
  63.         cout<<*it<<"     ";  
  64.     }  
  65.     cout<<endl;  
  66.     return 0;  
  67. }  

程序的運行結果如下:

 

上面的代碼用了4種方法建立vector並對其初始化

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

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

上述代碼對一個整形向量類進行操作,先定義一個整形元素向量類,然后插入3個值,並用3種不同的方法輸出,程序運行結果如下:
 

 

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


上述代碼通過定義元素為類的向量,通過插入3個初始化的類,並通過3種方法輸出,運行結果如下:

 

 

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


上述代碼通過定義元素為類指針的向量,通過插入3個初始化的類指針,並通過3種方法輸出指針指向的類,運行結果如下:

 

  3.修改元素示例

修改元素的方法主要有三種:1.通過數組修改,2.通過引用修改,3.通過迭代器修改
[cpp]  view plain  copy
 
  1. // vectorsample.cpp : 定義控制台應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include<iostream>  
  6. #include<vector>  
  7.   
  8. using namespace std;  
  9.   
  10.   
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.       
  14.     //int型vector,包含3個元素  
  15.     vector<int> vecIntA;  
  16.   
  17.     //插入1 2 3  
  18.     vecIntA.push_back(1);  
  19.     vecIntA.push_back(2);  
  20.     vecIntA.push_back(3);  
  21.       
  22.     int nSize = vecIntA.size();  
  23.   
  24.     //通過引用修改vector  
  25.     cout<<"通過數組修改,第二個元素為8:"<<endl;  
  26.     vecIntA[1]=8;  
  27.   
  28.     cout<<"vecIntA:"<<endl;  
  29.     //打印vectorA  
  30.     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
  31.     {  
  32.         cout<<*it<<"     ";  
  33.     }  
  34.     cout<<endl;  
  35.       
  36.     //通過引用修改vector  
  37.     cout<<"通過引用修改,第二個元素為18:"<<endl;  
  38.     int &m = vecIntA.at(1);  
  39.     m=18;  
  40.   
  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.   
  49.     //通過迭代器修改vector  
  50.     cout<<"通過迭代器修改,第二個元素為28"<<endl;  
  51.     vector<int>::iterator itr = vecIntA.begin()+1;  
  52.     *itr = 28;  
  53.   
  54.     cout<<"vecIntA:"<<endl;  
  55.     //打印vectorA  
  56.     for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)  
  57.     {  
  58.         cout<<*it<<"     ";  
  59.     }  
  60.     cout<<endl;  
  61.   
  62.     return 0;  
  63. }  

程序運行結果如下:
 

4.刪除向量示例

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

程序運行結果如下:
 

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.綜合示例

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

代碼運行實例如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM