C++ vector 容器淺析


一、什么是vector

向量(vector)是一個封裝了動態大小數組的順序容器(Sequence Container)。跟任意其它類型容器一樣,它能夠存放各種類型的對象。可以簡單的認為,向量是一個能夠存放任意類型的動態數組。

二、容器特性

1.順序序列

順序容器中的元素按照嚴格的線性順序排序。可以通過元素在序列中的位置訪問對應的元素。

2.動態數組

支持對序列中的任意元素進行快速直接訪問,甚至可以通過指針算述進行該操作。操供了在序列末尾相對快速地添加/刪除元素的操作。

3.能夠感知內存分配器的(Allocator-aware

容器使用一個內存分配器對象來動態地處理它的存儲需求。

三、基本函數實現

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)中元素設置成當前向量元素

8.看着清楚

  1. push_back 在數組的最后添加一個數據
  2. pop_back 去掉數組的最后一個數據
  3. at 得到編號位置的數據
  4. begin 得到數組頭的指針
  5. end 得到數組的最后一個單元+1的指針
  6. front 得到數組頭的引用
  7. back 得到數組的最后一個單元的引用
  8. max_size 得到vector最大可以是多大
  9. capacity 當前vector分配的大小
  10. size 當前使用數據的大小
  11. resize 改變當前使用數據的大小,如果它比當前使用的大,者填充默認值
  12. reserve 改變當前vecotr所分配空間的大小
  13. erase 刪除指針指向的數據項
  14. clear 清空當前的vector
  15. rbeginvector反轉后的開始指針返回(其實就是原來的end-1)
  16. rendvector反轉構的結束指針返回(其實就是原來的begin-1)
  17. empty 判斷vector是否為空
  18. swap 與另一個vector交換數據

四、基本用法

#include < vector> using namespace std; 

五、簡單介紹

Vector <類型> 標識符
Vector <類型> 標識符(最大容量)
Vector <類型> 標識符(最大容量, 初始所有值)
int i[5] = {1, 2, 3, 4, 5}
Vector <類型> vi(I, i+2); //得到i索引值為3以后的值
Vector < vector< int> > v; 二維向量//這里最外的<>要有空格。否則在比較舊的編譯器下無法通過
實例

1.pop_back()&push_back(elem) 實例在容器最后移除和插入數據
實例

#include <string.h> #include <vector> #include <iostream> using namespace std; int main() { vector<int>obj; //創建一個向量存儲容器 int for(int i=0; i<10; i++) // push_back(elem)在數組最后添加數據 { obj.push_back(i); cout << obj[i] << ","; } for(int i=0; i<5; i++)//去掉數組最后一個數據 { obj.pop_back(); } cout << "\n" << endl; for(int i=0; i<obj.size(); i++)//size()容器中實際數據個數 { cout << obj[i] << ","; } return 0; } 

輸出結果為:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

0, 1, 2, 3, 4,

  1. clear()清除容器中所以數據

實例

#include <string.h> #include <vector> #include <iostream> using namespace std; int main() { vector<int>obj; for(int i=0; i<10; i++)//push_back(elem)在數組最后添加數據 { obj.push_back(i); cout << obj[i] << ", "; } obj.clear(); //清除容器中所以數據 for(int i=0; i<obj.size(); i++) { cout << obj[i] << endl; } return 0; } 

輸出結果為:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

3.排序
實例

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int>obj; obj.push_back(1); obj.push_back(3); obj.push_back(0); sort(obj.begin(), obj.end()); //從小到大 cout << "從小到大 : " << endl; for(int i=0; i<obj.size(); i++) { cout << obj[i] << ", "; } cout << "\n" << endl; cout << "從大到小 : " << endl; reverse(obj.begin(), obj.end()); //從大到小 for(int i=0; i<obj.size(); i++) { cout << obj[i] << ", "; } return 0; } 

輸出結果為:

從小到大:
0, 1, 3,

從大到小:
3, 1, 0,

  1. 注意 sort 需要頭文件 #include <algorithm>
  2. 如果想 sort 來降序,可重寫sort
bool compare(int a, int b) { return a< b; //升序排列,如果改為return a>b,則為降序 } int a[20]={2, 4, 1, 23, 5, 76, 0, 43, 24, 65}, i; for(i=0; i<20; i++) cout << a[i] << endl; sort(a, a+20, compare); 

4. 訪問(直接數組訪問&迭代器訪問)
實例

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { //順序訪問 vector<int>obj; for(int i=0; i<10; i++) { obj.push_back(i); } cout << "直接利用數組:"; for(int i=0; i<10; i++)//方法一 { cout << obj[i] << " "; } cout << endl; cout << "利用迭代器:" ; //方法二,使用迭代器將容器中數據輸出 vector<int>::iterator it; //聲明一個迭代器,來訪問`vector`容器, //作用:遍歷或者指向`vector`容器的元素 for(it=obj.begin(); it!=obj.end(); it++) { cout << *it << " "; } return 0; } 

輸出結果為:

直接利用數組:0 1 2 3 4 5 6 7 8 9
利用迭代器:0 1 2 3 4 5 6 7 8 9

5. 二維數組兩種定義方法(結果一樣)

方法一

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N=5, M=6; vector<vector<int> > obj(N); //定義二維動態數組大小5行 for(int i =0; i< obj.size(); i++)//動態二維數組為5行6列,值全為0 { obj[i].resize(M); } for(int i=0; i< obj.size(); i++)//輸出二維動態數組 { for(int j=0; j<obj[i].size(); j++) { cout << obj[i][j] << " "; } cout << "\n"; } return 0; } 

方法二

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N=5, M=6; vector<vector<int> > obj(N, vector<int>(M)); //定義二維動態數組5行6列 for(int i=0; i< obj.size(); i++)//輸出二維動態數組 { for(int j=0; j<obj[i].size(); j++) { cout << obj[i][j] << " "; } cout << "\n"; } return 0; } 

輸出結果為:

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0


原文地址:http://blog.csdn.net/w_linux/article/details/71600574





免責聲明!

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



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