STL —— stack容器用法简介

一、简介

stack是一种容器适配器(STL的容器分为顺序容器和关联容器,容器适配器,是对这两类容器进行包装得到的具有更强的约束力的容器),被设计来用于操作先进后出(FILO)结构的情景,在这种情况下, 元素的插入和删除都只能在容器的尾部进行。

stack通过容器适配器来实现,是一种将特定的容器类作为其最底层的容器的类,它提供了一些特定的成员函数来访问自己的元素,元素只能在这个特定容器的后面,也就是栈的顶部,进行出栈和入栈操作。

最底层的容器可以是任意一种标准的容器模板类,或者是一些有明确目的的容器类,他们应该支持以下操作:

  • empty(判断是否为空)
  • size (返回栈的元素个数)
  • back (返回栈顶元素)
  • push_back (入栈)
  • pop_back (出栈)

标准的容器类,比如 vector,deque,list,满足以上需求。如果没有明确指定需要用的容器,默认情况下将会使用 deque。

二、函数用法示例

1、构造与析构(C++11版本)

initialize (1)          explicit stack (const container_type& ctnr);  
  
move-initialize (2)     explicit stack (container_type&& ctnr = container_type());  
  
allocator (3)           template <class Alloc> explicit stack (const Alloc& alloc);  
  
init + allocator (4)    template <class Alloc> stack (const container_type& ctnr, const Alloc& alloc);  
  
move-init + allocator (5)  template <class Alloc> stack (container_type&& ctnr, const Alloc& alloc);  
  
copy + allocator (6)    template <class Alloc> stack (const stack& x, const Alloc& alloc);  
  
move + allocator (7)    template <class Alloc> stack (stack&& x, const Alloc& alloc);  

(1)初始化构造方式
构造一个内部元素都是ctnr的拷贝的容器适配器

(2)move-initialization constructor
构造一个内部元素都是通过移动方式获得ctnr的值的容器适配器

cplusplus的例子:

// constructing stacks  
#include <iostream>       // std::cout  
#include <stack>          // std::stack  
#include <vector>         // std::vector  
#include <deque>          // std::deque  
  
int main ()  
{  
  std::deque<int> mydeque (3,100);          // deque with 3 elements  
  std::vector<int> myvector (2,200);        // vector with 2 elements  
  
  std::stack<int> first;                    // empty stack  
  std::stack<int> second (mydeque);         // stack initialized to copy of deque  
  
  std::stack<int,std::vector<int> > third;  // empty stack using vector  
  std::stack<int,std::vector<int> > fourth (myvector);  
  
  std::cout << "size of first: " << first.size() << '\n';  
  std::cout << "size of second: " << second.size() << '\n';  
  std::cout << "size of third: " << third.size() << '\n';  
  std::cout << "size of fourth: " << fourth.size() << '\n';  
  
  return 0;  
}  

2、empty()
返回当前栈是否为空(当它的大小是0的时候),empty()函数并不能清空栈,只是一个返回bool型的const函数

    stack<int>s;  
    s.push(1);  
    s.push(2);  
    cout << s.empty() << endl;   //输出0  

3、size()

返回容器中元素的个数,时间复杂度O(1),返回值类型是size_type,也就是unsigned int。size()函数不能改变栈的大小

4、top()
返回栈顶元素的引用。
由于栈是一种先进后出的结构,所以最顶部的元素就是最后插入栈的元素。
( C++11中会自动根据元素类型返回reference或者是const_reference,对一个空的栈调用top函数,会异常终止,所以应该使用empty()函数提前检查)

stack<int>s;  
s.push(1);  
s.push(2);  
cout << s.top() << endl;    //输出2  
s.top() += 3;               //引用可以作为左值  
cout << s.top() << endl;    //输出5  

5、push() 和 emplace() (C++11)

push()函数和 emplace()都是在栈这个容器的顶部插入一个新的元素。
push(),实际上是调用的底层容器的 push_back()函数,新元素的值是 push函数参数的一个拷贝。
emplace(),实际上是调用的底层容器的 emplace_back()函数,新元素的值是在容器内部就地构造的,不需要移动或者拷贝。
stack的 emplace也可以用在普通的基本类型上。

struct Node  
{  
    Node(int x)  
        :x(x){}  
    int x;  
};  
  
int main()  
{  
    stack<Node>s1;  
    s1.emplace(1);  
    s1.push(Node(2));  
  
    stack<int>s2;  
    s2.emplace(1);   //OK  
    return 0;  
}  

6、pop()

删除最顶部的元素,使栈的大小减小

这个被删除的元素,是刚刚插入栈的元素,这个元素和top函数的返回值是一致的。这个函数会调用对象的析构函数(如果有的话),pop()实际上是用过底层容器的pop_back()函数实现的。

(对一个空栈进行pop(),会导致程序异常终止,应该使用empty提前检查)

stack<int>s;  
s.push(1);  
s.push(2);  
s.push(3);  
  
cout << s.top() << endl;   //3  
s.pop();  
cout << s.top() << endl;   //2  

栈没有clear或者erase函数,如果想要清空一个栈,需要循环的调用出栈函数。


stack<int>s;  
//s.erase(); //error  
//s.clear(); //error  
s.push(1);  
s.push(2);  
s.push(3);  
cout << s.size() << endl;   //3  
while(!s.empty())  
    s.pop();  
cout << s.size() << endl;    //0  

7、swap()

交换两个栈的内容(所有元素),这个函数通过非成员函数swap()来交换底层容器,时间复杂度O(1)

cplusplus的例子

// stack::swap  
#include <iostream>       // std::cout  
#include <stack>          // std::stack  
  
int main ()  
{  
  std::stack<int> foo,bar;  
  foo.push (10); foo.push(20); foo.push(30);  
  bar.push (111); bar.push(222);  
  
  foo.swap(bar);  
  
  std::cout << "size of foo: " << foo.size() << '\n';  
  std::cout << "size of bar: " << bar.size() << '\n';  
  
  return 0;  
}  

三、结语

栈是一种特别常用的数据结构,在各种编程语言或者是操作系统内部,都能见到。常见的DFS算法,递归算法;上下文切换,进程调度,以及寄存器本身。

本文转载于http://blog.csdn.net/gscsdlz/article/details/52169463
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值