c++ stlstack介绍

C++ Stack(堆栈) 是一个容器的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。

c++ stl栈stack的头文件:

#include <stack>

c++ stl栈stack的成员函数介绍

操作 比较和分配堆栈

empty() 堆栈为空则返回真

pop() 移除栈顶元素

push() 在栈顶增加元素

size() 返回栈中元素数目

top() 返回栈顶元素

stack 介绍

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

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

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

  1. <span style="font-size:16px;"><strong>•</strong>back()
  2. •push_back()
  3. •pop_back()</span>

因此,标准的容器类模板vectordeque 和list可以使用,默认情况下,如果没有容器类被指定成为一个提别的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).
  1. // test_stack.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <stack>
  5. #include <vector>
  6. #include <deque>
  7. #include <iostream>
  8. using namespace std;
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. deque<int> mydeque(2,100);
  12. vector<int> myvector(2,200);
  13. stack<int> first;
  14. stack<int> second(mydeque);
  15. stack<int,vector<int> > third;
  16. stack<int,vector<int> > fourth(myvector);
  17. cout << "size of first: " << (int) first.size() << endl;
  18. cout << "size of second: " << (int) second.size() << endl;
  19. cout << "size of third: " << (int) third.size() << endl;
  20. cout << "size of fourth: " << (int) fourth.size() << endl;
  21. return 0;
  22. }

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 0false otherwise.

  1. // stack::empty
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5. int main ()
  6. {
  7. stack<int> mystack;
  8. int sum (0);
  9. for (int i=1;i<=10;i++) mystack.push(i);
  10. while (!mystack.empty())
  11. {
  12. sum += mystack.top();
  13. mystack.pop();
  14. }
  15. cout << "total: " << sum << endl;
  16. return 0;
  17. }

Output:

total: 55

stack::pop

void pop ( );

在栈的顶部移除元素。

  1. // stack::push/pop
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5. int main ()
  6. {
  7. stack<int> mystack;
  8. for (int i=0; i<5; ++i) mystack.push(i);
  9. cout << "Popping out elements...";
  10. while (!mystack.empty())
  11. {
  12. cout << " " << mystack.top();
  13. mystack.pop();
  14. }
  15. cout << endl;
  16. return 0;
  17. }

Output:

Popping out elements... 4 3 2 1 0

stack::push

 
void push ( const T& x );

在栈顶添加元素

  1. // stack::push/pop
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5. int main ()
  6. {
  7. stack<int> mystack;
  8. for (int i=0; i<5; ++i) mystack.push(i);
  9. cout << "Popping out elements...";
  10. while (!mystack.empty())
  11. {
  12. cout << " " << mystack.top();
  13. mystack.pop();
  14. }
  15. cout << endl;
  16. return 0;
  17. }

Output:

Popping out elements... 4 3 2 1 0

stack::size

 
 
size_type size ( ) const;

计算栈对象元素个数

  1. // stack::size
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5. int main ()
  6. {
  7. stack<int> myints;
  8. cout << "0. size: " << (int) myints.size() << endl;
  9. for (int i=0; i<5; i++) myints.push(i);
  10. cout << "1. size: " << (int) myints.size() << endl;
  11. myints.pop();
  12. cout << "2. size: " << (int) myints.size() << endl;
  13. return 0;
  14. }

Output:

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

stack::top

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

返回栈顶元素

  1. // test_stack.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <stack>
  5. #include <vector>
  6. #include <deque>
  7. #include <iostream>
  8. using namespace std;
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. stack<int> mystack;
  12. mystack.push(10);
  13. mystack.push(20);
  14. mystack.top()-=5;
  15. cout << "mystack.top() is now " << mystack.top() << endl;
  16. return 0;
  17. }

Output:

mystack.top() is now 15

10-stack的更多相关文章

  1. [CareerCup] 9.10 Stack Boxes 垒箱子问题

    9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotat ...

  2. 给jdk写注释系列之jdk1.6容器(10)-Stack&Vector源码解析

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈.      什么是栈呢,我就不找它具体的定义了,直接举个例子,栈就相当于一个很窄的木桶 ...

  3. C# 队列(Queue)和 堆栈(Stack)

    C# 队列(Queue)和 堆栈(Stack) C# 队列(Queue) 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队 ...

  4. 在 WPF 程序中应用 Windows 10 真?亚克力效果

    原文:在 WPF 程序中应用 Windows 10 真?亚克力效果 从 Windows 10 (1803) 开始,Win32 应用也可以有 API 来实现原生的亚克力效果了.不过相比于 UWP 来说, ...

  5. 1057 Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  6. Go语言的堆栈分析

    本文为理解翻译,原文地址:http://www.goinggo.net/2015/01/stack-traces-in-go.html Introduction 在Go语言中有一些调试技巧能帮助我们快 ...

  7. 可爱的Python_课后习题_CDay−2 完成核心功能

    1. 在前文的grep 实现例子中,没有考虑子目录的处理方式,因为如果直接open 目录进行读grep 是古老实用且高效的模式文本匹配工具,在所有的Unix/Linux 系统中都会默认安装,它最常做的 ...

  8. 【转】 C++模板详解

    C++模板 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 通常有 ...

  9. jdk 编译器 对final字段的处理

    class FinalTest{     void a(){         final int i=10;         int j=10;     } }            stack=2, ...

  10. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. c# 数据库通用类DbUtility

    DbProviderType数据库类型枚举 /// <summary> /// 数据库类型枚举 /// </summary> public enum DbProviderTyp ...

  2. 从汇编的角度看待const与#define

    先观察一下的代码: #include<stdio.h> int main(){ ; int y; int *pi=(int*)&i; *pi=; y=*pi; int tempi; ...

  3. C/S模式与B/S模式的详细介绍

    网络程序开发的两种计算模式--C/S模式与B/S模式.两种各有千秋,用于不同场合. C/S适用于专人使用,安全性要求较高的系统: B/S适用于交互性比较频繁的场合,容易被人们所接受,倍受用户和软件开发 ...

  4. Oracle拆分字符串函数与执行调用

    本函数可以将“目标字符串”以“指定字符串”进行拆分,并通过表结构返回结果.代码如下: ); CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHA ...

  5. [欢乐赛]班服 状压DP

    班服 (shirt.pas/.c/.cpp) 时间限制:1s:内存限制 128MB 题目描述: 要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100.现在每个班都挑出了一些样 ...

  6. Linux命令-chmod、chown和chgrp

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

  7. GTID 跳过脚本

    跳过单个error STOP SLAVE; SET gtid_next = '3b977b7e-ed28-11e7-a8ff-b4969113b678:138609841'; BEGIN;COMMIT ...

  8. mydumper 找不到libmysqlclient.so.20

    报错信息: mydumper: error while loading shared libraries: libmysqlclient.so.20: cannot open shared objec ...

  9. Django前端的文本编辑器,滑动登陆

    文本编译器 # 添加文章 url(r'^addarticle/$', views.addarticle), # 利用文本编辑器添加文章 def addarticle(request): ''' 添加文 ...

  10. android流畅度比Ios差的原因

    Andorid更新了一个版本又一个版本,硬件从单核到双核到四核,系统流畅度总算基本能和iOS持平了.不过人们不禁会问,为什么都是基于Linux,两个系统会差别如此大?为什么iPhone 4用单核处理器 ...