標準模板庫STL中vector使用參考手冊


簡介:

  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


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章