1. 程式人生 > >zhaoguangyao

zhaoguangyao

vector

一、概念

首先要知道,vector是一個class template,他申請的是一塊連續的地址空間。

Vectors are sequence containers representing arrays that can change in size.

向量(Vector)是一個封裝了動態大小陣列的順序容器(Sequence Container)。跟任意其它型別容器一樣,它能夠存放各種型別的物件。可以簡單的認為,向量是一個能夠存放任意型別的動態陣列。

說明: Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

二、容器的屬性

1、Sequence(順序序列)

Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

順序容器中的元素按照嚴格的線性順序排序。可以通過元素在序列中的位置訪問對應的元素。 2、Dynamic array(動態陣列)

Allows direct access to any element in the sequence, even through pointer arithmetics, and provides relatively fast addition/removal of elements at the end of the sequence.

支援對序列中的任意元素進行快速直接訪問,甚至可以通過指標算述進行該操作。操供了在序列末尾相對快速地新增/刪除元素的操作。 3、Allocator-aware(能夠感知記憶體分配器的)

The container uses an allocator object to dynamically handle its storage needs.

容器使用一個記憶體分配器物件來動態地處理它的儲存需求。

三、成員函式

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

四、程式碼例項