Vector相似於一個動態數組前端
使用vector,需加入頭文件:ios
#include <vector>
Vector複製全部元素到它的內部動態數組中。一般這些元素是有必定順序的,所以vector是一種有序集合。數組
Vector提供隨機訪問,若是知道元素的位置,能夠直接訪問元素。app
當在尾部插入和刪除元素時,vector表現很好;但在中間或前端插入、刪除元素時,則較慢。函數
Vector除了提供經常使用的操做size(),empty(),和max_size(),還提供了一個capacity()函數,用來返回一個vector在它的實際內存中所包含的元素個數。一旦超出了這個capacity,vector將重新分配它的內部內存。spa
capacity的重要性有兩個方面:指針
爲了不再分配(reallocation),可使用reserve()函數來確保capacity:code
vector<int> v; //create an empty vector v.reserve(80); //reserve memory for 80 elements
調用reserve()並不能收縮容量(capacity)。若是調用reserve()的參數小於當前的capacity,那麼這個操做是no-op。索引
若是沒有調用reserve()設置容量,在插入數據前,將分配一塊內存(例如2K)來存放數據,這樣就會形成內存浪費。內存
C++11介紹了一個新的函數:a nonbinding request to shrink the capacity to fit the current number of elements
v.shrink_to_fit(); //request to shrink memory
Operation | Effect |
vector<Elem> c | Default constructor:建立一個空的vector |
vector<Elem> c(c2) | Copy constructor:經過複製c2建立c |
vector<Elem> c = c2 | Copy constructor:經過複製c2建立c |
vector<Elem> c(rv) | Move constructor:creates a new vector,taking the contents of the rvalue rv |
vector<Elem> c = rv | Move constructor:creates a new vector,taking the contents of the rvalue rv |
vector<Elem> c(n) | 使用默認構造函數建立一個有n個元素的vector |
vector<Elem> c(n, elem) | 建立一個有n個元素的vector,而且使用elem初始化 |
vector<Elem> c(beg, end) | 建立一個使用範圍[beg,end)初始化的vector |
vector<Elem> c(initList) | 建立一個使用initList初始化的vector |
vector<Elem> c = initList | 建立一個使用initList初始化的vector |
c.~vector() | 銷燬全部元素並釋放內存 |
Operation | Effect |
c.empty() | 返回容器是否爲空(至關於size() == 0) |
c.size() | 返回當前元素的個數 |
c.max_size() | 返回可能存在元素的最大個數 |
c.capacity() | 返回沒有再分配的元素的最大個數 |
c.reserve(num) | 擴展capacity |
c.shrink_to_fit() | 收縮capacity,適應元素個數 |
c1 == c2 | 返回c1是否等於c2 |
c1 != c2 | 返回c1是否不等於c2 |
c1 < c2 | 返回c1是否小於c2 |
c1 > c2 | 返回c1是否大於c2 |
c1 <= c2 | 返回c1是否小於等於c2 |
c1 >= c2 | 返回c1是否大於等於c2 |
Operation | Effect |
c = c2 | 將c2全部的元素賦給c |
c = rv | Move assigns all elements of the rvalue rv to c |
c = initList | 將initList賦給c |
c.assign(n, elem) | 將值val賦給c的每個元素 |
c.assign(beg, end) | 將[beg,end)範圍的值分配給c |
c.assign(initList) | 將initList的值分配給c |
c1.swap(c2) | 交換c1和c2的數據 |
swap(c1, c2) | 交換c1和c2的數據 |
Operation | Effect |
c[idx] | 返回索引爲idx的元素(沒有邊界檢查) |
c.at(idx) | 返回索引爲idx的元素(當idx超出邊界,拋出range-error異常) |
c.front() | 返回第一個元素(不檢查第一個元素是否存在) |
c.back() | 返回最後一個元素(不檢查最後一個元素是否存在) |
Operation | Effect |
c.push_back(elem) | 將elem附加到末尾 |
c.pop_back() | 移除最後一個元素,不返回 |
c.insert(pos, elem) | 將elem插入到pos以前的位置,並返回新元素的位置 |
c.insert(pos, n, elem) | 在pos以前的位置開始插入n個elem,並返回新插入的第一個元素的位置 |
c.insert(pos, beg, end) | 將[beg, end)的值插入到從pos以前的位置,並返回第一個新元素的位置 |
c.insert(pos, initlist) | 將initlist的值插入到從pos以前的位置,並返回第一個新元素的位置 |
c.emplace(pos, args...) | 將args插入到pos以前的位置,並返回第一個新元素的位置 |
c.emplace_back(args...) | 將args附加到末尾 |
c.erase(pos) | 移除pos所指的元素,並返回下一個元素位置 |
c.erase(beg, end) | 移除範圍[beg,end)的元素,並返回下一個元素的位置 |
c.resize(num) | 設置元素個數爲num,若是增加了,使用默認構造函數初始化 |
c.resize(num, elem) | 設置元素個數爲num,若是增加了,使用elem初始化 |
c.clear() | 移除全部元素 |
例如,移除第一個元素:
vector<Elem> coll; ... //remove first element with value val vector<Elem>::iterator pos; pos = find(coll.begin(), coll.end(), val); if(pos != coll.end()) { coll.earse(pos); }
#include<vector> #include<iostream> #include<string> #include<algorithm> #include<iterator> using namespace std; int main() { //create empty vector for stirng vector<string> sentence; //reserve memory for elements to avoid reallocation sentence.reserve(5); //append some element sentence.push_back("Hello,"); sentence.insert(sentence.end(), { "how", "are", "you", "?"}); //print elements separated with spaces copy(sentence.cbegin(), sentence.cend(), ostream_iterator<string>(cout, " ")); cout << endl; //print "technical data" cout << "max_size(): " << sentence.max_size() << endl; cout << "size(): " << sentence.size() << endl; cout << "capacity(): " << sentence.capacity() << endl; //swap second and fourth element swap(sentence[1], sentence[3]); //insert element "always" before element "?" sentence.insert(find(sentence.begin(), sentence.end(), "?"), "always"); //assign "!" to the last element sentence.back() = "!"; //print elements separated with space copy(sentence.cbegin(), sentence.cend(), ostream_iterator<string>(cout, " ")); cout << endl; //print some technical data again cout << "size(): " << sentence.size() << endl; cout << "capacity(): " << sentence.capacity() << endl; //delete last two elements sentence.pop_back(); sentence.pop_back(); //shrink capacity sentence.shrink_to_fit(); //print some technical data again cout << "size(): " << sentence.size() << endl; cout << "capacity(): " << sentence.capacity() << endl; }
輸出: