织梦CMS - 轻松建站从此开始!

IT900学习网

当前位置: 主页 > 应用程序 >

【c++】STL之stack详细介绍

时间:2011-10-24 21:45来源:未知 作者:admin 点击:

stack 介绍

栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出

栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。

这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作

 

因此,标准的容器类模板vector, dequelist可以使用,默认情况下,如果没有容器类被指定成为一个提别的stack 类,标准的容器类模板就是deque 队列。


实现C++  STL,栈有两个参数。

template < class T, class Container = deque<T> > class stack;

参数示意:

  • T: 元素类型
  • Container: 被用于存储和访问元素的的类型

成员函数

stack::stack

explicit stack ( const Container& ctnr = Container() );

用于构造一个栈适配器对象。

ctnr
Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).

output:

size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

stack::empty

bool empty ( ) const;

判断是否为空。

Return Value

true if the container size is 0, false otherwise.

Output:

total: 55

stack::pop

void pop ( );

在栈的顶部移除元素。

 


 

Output:

Popping out elements... 4 3 2 1 0

 

stack::push

void push ( const T& x );

在栈顶添加元素


Output:

Popping out elements... 4 3 2 1 0

stack::size

 
size_type size ( ) const;

计算栈对象元素个数

 




Output:

0. size: 0
1. size: 5
2. size: 4

stack::top

 
      value_type& top ( );
const value_type& top ( ) const;

返回栈顶元素

Output:

mystack.top() is now 15

 

(责任编辑:qhgongzi)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片