10-stack
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
c++ stl栈stack的头文件为:
#include <stack>
c++ stl栈stack的成员函数介绍
操作 比较和分配堆栈
empty() 堆栈为空则返回真
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
stack 介绍
栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出
栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。
这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作
- <span style="font-size:16px;"><strong>•</strong>back()
- •push_back()
- •pop_back()</span>
因此,标准的容器类模板vector, deque 和list可以使用,默认情况下,如果没有容器类被指定成为一个提别的stack 类,标准的容器类模板就是deque 队列。
实现C++ STL,栈有两个参数。
|
参数示意:
- 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).
- // test_stack.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stack>
- #include <vector>
- #include <deque>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- deque<int> mydeque(2,100);
- vector<int> myvector(2,200);
- stack<int> first;
- stack<int> second(mydeque);
- stack<int,vector<int> > third;
- stack<int,vector<int> > fourth(myvector);
- cout << "size of first: " << (int) first.size() << endl;
- cout << "size of second: " << (int) second.size() << endl;
- cout << "size of third: " << (int) third.size() << endl;
- cout << "size of fourth: " << (int) fourth.size() << endl;
- return 0;
- }
output:
size of first: 0 |
stack::empty
bool empty ( ) const;
判断是否为空。
Return Value
true if the container size is 0, false otherwise.
- // stack::empty
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- int sum (0);
- for (int i=1;i<=10;i++) mystack.push(i);
- while (!mystack.empty())
- {
- sum += mystack.top();
- mystack.pop();
- }
- cout << "total: " << sum << endl;
- return 0;
- }
Output:
total: 55 |
stack::pop
void pop ( );
在栈的顶部移除元素。
- // stack::push/pop
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- for (int i=0; i<5; ++i) mystack.push(i);
- cout << "Popping out elements...";
- while (!mystack.empty())
- {
- cout << " " << mystack.top();
- mystack.pop();
- }
- cout << endl;
- return 0;
- }
Output:
Popping out elements... 4 3 2 1 0 |
stack::push
void push ( const T& x );
在栈顶添加元素
- // stack::push/pop
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- for (int i=0; i<5; ++i) mystack.push(i);
- cout << "Popping out elements...";
- while (!mystack.empty())
- {
- cout << " " << mystack.top();
- mystack.pop();
- }
- cout << endl;
- return 0;
- }
Output:
Popping out elements... 4 3 2 1 0 |
stack::size
size_type size ( ) const;
计算栈对象元素个数
|
Output:
0. size: 0 |
stack::top
value_type& top ( );
const value_type& top ( ) const;
返回栈顶元素
- // test_stack.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stack>
- #include <vector>
- #include <deque>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- stack<int> mystack;
- mystack.push(10);
- mystack.push(20);
- mystack.top()-=5;
- cout << "mystack.top() is now " << mystack.top() << endl;
- return 0;
- }
Output:
mystack.top() is now 15 |
10-stack的更多相关文章
- [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 ...
- 给jdk写注释系列之jdk1.6容器(10)-Stack&Vector源码解析
前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈. 什么是栈呢,我就不找它具体的定义了,直接举个例子,栈就相当于一个很窄的木桶 ...
- C# 队列(Queue)和 堆栈(Stack)
C# 队列(Queue)和 堆栈(Stack) C# 队列(Queue) 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队 ...
- 在 WPF 程序中应用 Windows 10 真?亚克力效果
原文:在 WPF 程序中应用 Windows 10 真?亚克力效果 从 Windows 10 (1803) 开始,Win32 应用也可以有 API 来实现原生的亚克力效果了.不过相比于 UWP 来说, ...
- 1057 Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- Go语言的堆栈分析
本文为理解翻译,原文地址:http://www.goinggo.net/2015/01/stack-traces-in-go.html Introduction 在Go语言中有一些调试技巧能帮助我们快 ...
- 可爱的Python_课后习题_CDay−2 完成核心功能
1. 在前文的grep 实现例子中,没有考虑子目录的处理方式,因为如果直接open 目录进行读grep 是古老实用且高效的模式文本匹配工具,在所有的Unix/Linux 系统中都会默认安装,它最常做的 ...
- 【转】 C++模板详解
C++模板 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 通常有 ...
- jdk 编译器 对final字段的处理
class FinalTest{ void a(){ final int i=10; int j=10; } } stack=2, ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- requirejs 到底有什么好处?
无论是在backbone时代,还是angularjs 时代 我都用过requirejs, 后来慢慢全都去掉了, 因为在前端开发requirejs 感觉没有带来任何实质性的好处. 从几个方面说说我的感受 ...
- proxool 连接池
今天配置proxool 连接池,发现可配置属性非常多,以前也只是用,没总结过,今天查了下网上的资料,总结一下 方便你我.其实网上很多英文资料都很全,网上很多人就是考翻译老外的文章赚些流量,其实也没啥意 ...
- shell常用测试命令
预定义变量: 预定义变量是由Bash程序预先定义好的一类特殊变量,用户只能使用预定义变量,而不能创建新的预定义变量,也不能直接为预定义变量赋值.预定义比变量使用"$"符合和另一个符 ...
- GOF23设计模式之中介者模式(mediator)
一.中介者模式概述 如果一个系统中对象之间的联系呈现网状结构,对象之间存在大量多对多的关系,导致关系及其复杂,这时可以引入一个中介者对象,使得各个对象只跟中介者对象打交道,从而将复杂的网络结构化为星型 ...
- 【原】C++11并行计算 — 数组求和
本文转载请注明出处 -- polobymulberry-博客园 0x00 - 前言 最近想优化ORB-SLAM2,准备使用并行计算来提高其中ORB特征提取的速度.之前对并行计算方面一窍不通.借此机会, ...
- HBase之五:hbase的region分区
一.Region 概念 Region是表获取和分布的基本元素,由每个列族的一个Store组成.对象层级图如下: Table (HBase table) Region (Regions for the ...
- 一行代码搞定 FTP 服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib/ ...
- 4_python之路之模拟工资管理系统
python之路之模拟工资管理系统 1.程序说明:Readme.txt 1.程序文件:salary_management.py info.txt 2.程序文件说明:salary_management. ...
- Tkinter Spinbox
Python - Tkinter Spinbox: Spinbox小部件是一个标准的Tkinter的Entry小窗口部件的变体,它可以用来选择从一个固定的值. Spinbox小部件是一个标准的Tk ...
- C++获取本机的ip地址程序
#include <WinSock2.h> #pragma comment(lib,"ws2_32") //链接到ws2_32动态链接库 class CInitSock ...