簡介:
vector之所以被認為是一個容器,是因為它能夠像容器一樣存放各種類型的對象,簡單地說,vector是一個能夠存放任意類型的動態數組,能夠增加和壓縮資料。
為了可以使用vector,必須在你的標頭檔中包含下面的代碼:
#include <vector> 注意:標頭檔沒有“.h”
vector屬於std命名域的,因此需要通過命名限定,如下完成你的代碼:
using std::vector;
vector<int> vInts;
或者連在一起,使用全名:
std::vector<int> vInts;
建議在代碼量不大,並且使用的命名空間不多的情況下,使用全域的命名域方式:
using namespace std;
構造:
這個建構函式還有一個可選的參數,這是一個類型為T的執行個體,描述了各個向量種各成員的初始值;
如:vector<int> v2(init_size,0); 如果預先定義了:int init_size; 他的成員值都被初始化為0;
複製建構函式,構造一個新的向量,作為已存在的向量的完全複製;
如:vector<int> v3(v2);
帶兩個常量參數的建構函式,產生初始值為一個區間的向量。區間由一個半開區間[first,last)來指定。
如:vector<int> v4 (first,last)
#include <vector> //The default vector constructor takes no arguments, creates a new instance of that vector. vector(); //The second constructor is a default copy constructor that can be used to create //a new vector that is a copy of the given vector c. vector( const vector& c ); //The third constructor creates a vector with space for num objects. val is specified, //each of those objects will be given that value. For example, the following code creates //a vector consisting of five copies of the integer 42:vector<int> v1( 5, 42 ); vector( size_type num, const TYPE& val = TYPE() ); //The last constructor creates a vector that is initialized to contain the elements end vector( input_iterator start, input_iterator end ); ~vector();
成員函數:
| |
|
| |
|
| assign |
assign elements to a vector |
| at |
returns an element at a specific location |
| back |
returns a reference to last element of a vector |
| begin |
returns an iterator to the beginning of the vector |
| capacity |
returns the number of elements that the vector can hold |
| clear |
removes all elements from the vector |
| empty |
true if the vector has no elements |
| end |
returns an iterator just past the last element of a vector |
| erase |
removes elements from a vector |
| front |
returns a reference to the first element of a vector |
| insert |
inserts elements into the vector |
| max_size |
returns the maximum number of elements that the vector can hold |
| pop_back |
removes the last element of a vector |
| push_back |
add an element to the end of the vector |
| rbegin |
returns a reverse_iterator to the end of the vector |
| rend |
returns a reverse_iterator to the beginning of the vector |
| reserve |
sets the minimum capacity of the vector |
| resize |
change the size of the vector |
| size |
returns the number of items in the vector |
| swap |
swap the contents of this vector with another |
用中文的表格就是:
c.assign(beg,end) |
將[beg; end)區間中的資料賦值給c |
c.assign(n,elem) |
將n個elem的拷貝賦值給c |
c.at(idx) |
傳回索引idx所指的資料,如果idx越界,拋出out_of_range |
c.back() |
傳回最後一個資料,不檢查這個資料是否存在 |
c.begin() |
傳回迭代器中的第一個資料地址 |
c.size() |
返回容器中實際資料的個數 |
c.end() |
指向迭代器中末端元素的下一個,指向一個不存在元素 |
c.clear() |
移除容器中所有資料 |
c.empty() |
判斷容器是否為空白 |
c.erase(pos) |
刪除pos位置的資料,傳回下一個資料的位置 |
c.erase(beg,end) |
刪除[beg,end)區間的資料,傳回下一個資料的位置 |
c.front() |
傳回第一個資料 |
c.insert(pos,elem) |
在pos位置插入一個elem拷貝,傳回新資料位元置 |
c.insert(pos,n,elem) |
在pos位置插入n個elem資料。無傳回值 |
c.insert(pos,beg,end) |
在pos位置插入在[beg,end)區間的資料。無傳回值 |
c.max_size() |
返回容器中最大資料的數量 |
c.pop_back() |
刪除最後一個資料 |
c.push_back(elem) |
在尾部加入一個資料 |
c.rbegin() |
傳回一個逆向隊列的第一個資料 |
c.rend() |
傳回一個逆向隊列的最後一個資料的下一個位置 |
c.resize(num) |
重新指定隊列的長度 |
c.reserve() |
保留適當的容量 |
c1.swap(c2) |
將c1和c2元素互換 |
swap(c1,c2) |
將c1和c2元素互換 |
|
|
用容器實現堆棧:
#ifndef STACK#define STACK#include <vector>template<class T, int capacity = 30>class Stack{public:Stack()//建構函式{pool.reserve(capacity);//設定容器最小的容量}void clear()//清空棧{pool.clear();//清空容器}bool isEmpty() const//判斷棧是否為空白{return pool.empty();//容器是否為空白}T& topEl()//取棧頂元素{return pool.back();//取容器最後一個元素}T pop()//出棧{T el = pool.back();//取容器最後一個元素pool.pop_back();//刪除最後一個元素return el;}void push(const T& el)//進棧{pool.push_back(el);//在容器的最末尾增加一個元素}private:vector<T> pool;};#endif