标准模板库STL中vector使用参考手册

来源:互联网 发布:ip端口打开失败 编辑:程序博客网 时间:2024/05/15 04:02


简介:

  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();    

 

成员函数:

    assignassign elements to a vectoratreturns an element at a specific locationbackreturns a reference to last element of a vectorbeginreturns an iterator to the beginning of the vectorcapacityreturns the number of elements that the vector can holdclearremoves all elements from the vectoremptytrue if the vector has no elementsendreturns an iterator just past the last element of a vectoreraseremoves elements from a vectorfrontreturns a reference to the first element of a vectorinsertinserts elements into the vectormax_sizereturns the maximum number of elements that the vector can holdpop_backremoves the last element of a vectorpush_backadd an element to the end of the vectorrbeginreturns a reverse_iterator to the end of the vectorrendreturns a reverse_iterator to the beginning of the vectorreservesets the minimum capacity of the vectorresizechange the size of the vectorsizereturns the number of items in the vectorswapswap 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


 

原创粉丝点击