标准模板库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();    

 

成员函数:

  
  
assignassign elements to a vector
atreturns an element at a specific location
backreturns a reference to last element of a vector
beginreturns an iterator to the beginning of the vector
capacityreturns the number of elements that the vector can hold
clearremoves all elements from the vector
emptytrue if the vector has no elements
endreturns an iterator just past the last element of a vector
eraseremoves elements from a vector
frontreturns a reference to the first element of a vector
insertinserts elements into the vector
max_sizereturns the maximum number of elements that the vector can hold
pop_backremoves the last element of a vector
push_backadd an element to the end of the vector
rbeginreturns a reverse_iterator to the end of the vector
rendreturns a reverse_iterator to the beginning of the vector
reservesets the minimum capacity of the vector
resizechange the size of the vector
sizereturns the number of items in the vector
swapswap 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


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值