stack--概述:
  栈(Stack)是一种特殊的线性表,只能在某一端插入和删除的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶。栈也称为先进后出表(LIFO)。
允许进行插入和删除操作的一端称为栈顶(Top),另一端为栈底(Bottom)。栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。
l插入一个元素称为进栈(Push),删除一个栈顶元素称为出栈(Pop)。
                              
 

成员函数

功能

bool   empty()

栈为空返回true,否则返回false

void   pop()

删除栈顶元素,即出栈

void push( const   TYPE &val   )

将新元素val进栈,使其成为栈顶的第一个元素

TYPE   &top()

查看当前栈顶元素

size_type   size()

返回堆栈中的元素数目

 
题目练习:
(会陆续添加)
1.stack模拟题, 要读懂题意(别忘了清空栈)
 #include<iostream>
#include<string>
#include<stack>
using namespace std; int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
stack<string> s1;
stack<string> s2;
string str, str2;
string top;
int n;
cin>>n;
while(n--)
{
while(!s1.empty()) s1.pop();
while(!s2.empty()) s2.pop();
s2.push("http://www.acm.org/");
while(cin>>str&&str!="QUIT")
{
if(str=="VISIT")
{
while(!s1.empty()) s1.pop();
cin>>str2;
s2.push(str2);
cout<<str2<<endl;
}
if(str=="BACK")
{
if(s2.size()==) cout<<"Ignored\n";
else
{
top = s2.top();
s2.pop();
s1.push(top);
top = s2.top();
cout<<top<<endl;
}
}
if(str=="FORWARD")
{
if(s1.empty()) cout<<"Ignored\n";
else
{
top = s1.top();
s1.pop();
s2.push(top);
cout<<top<<endl;
}
}
}
if(n) cout<<endl;
}
return ;
}
2.一道较综合性的模拟题:
 //解题思路, 用栈存储矩阵信息(行和列),
// 遇到右括号进行栈顶两个元素相乘,
//并把乘积入栈。 #include<iostream>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
using namespace std; struct Node{
int row;
int col;
}; map<char, Node> matrix;
int n;
char name; int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
cin>>n;
for(int i=; i<n; i++)
{
cin>>name;
cin>>matrix[name].row>>matrix[name].col;
}
string exp;
while(cin>>exp)
{
int i;
int count = ;
stack<Node> array;
for(i=; i<exp.size(); i++)
{
if(exp[i]=='(') continue;
if(exp[i]==')')
{
Node b = array.top();
array.pop();
Node a = array.top();
array.pop();
if(a.col!=b.row)
{
cout<<"error"<<endl;
break;
}
count += a.row*b.row*b.col;
Node tmp = {a.row, b.col};
array.push(tmp);
}
else array.push(matrix[exp[i]]);
}
if(i==exp.size())
cout<<count<<endl;
}
return ;
}

3.注意读入技巧。

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1259

 #include<iostream>
#include<stack>
using namespace std; const int maxn = + ;
int target[maxn], n; void go(int *target)
{
int A=, B=;
stack<int> s;
int ok=;
while(B<=n)
{
if(A==target[B])
{
A++, B++;
}
else if(!s.empty()&&s.top()==target[B])
{
s.pop(); B++;
}
else if(A<=n) s.push(A++);
else
{
ok = ; break;
}
}
if(ok) printf("Yes\n");
else printf("No\n");
} int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
while(scanf("%d", &n)!=EOF&&n)
{
RL: scanf("%d", &target[]);
if(!target[])
{
printf("\n");
continue;
}
else
{
for(int i=; i<=n; i++)
scanf("%d", &target[i]);
go(target); goto RL;
}
}
return ;
}

4.再来道稍难的(stack+回溯法)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004

 #include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
using namespace std; string a, b;
stack<char> build;
vector<char> operate;
int len; void dfs(int A, int B)
{
if(A==len&&B==len)
{
for(int i=; i<operate.size(); i++)
cout<<operate[i]<<" ";
cout<<endl;
}
if(A+<=len)
{
build.push(a[A]);
operate.push_back('i');
dfs(A+, B);
build.pop();
operate.pop_back();
}
if(B+<=A&&B+<=len&&build.top()==b[B])
{
char tc = build.top();
build.pop();
operate.push_back('o');
dfs(A, B+);
build.push(tc);
operate.pop_back();
}
}
int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
while(cin>>a>>b)
{
len = a.length();
cout<<"["<<endl;
dfs(, );
cout<<"]"<<endl;
}
return ;
}

STL--stack的更多相关文章

  1. STL stack 容器

    STL stack 容器 Stack简介 stack是堆栈容器,是一种“先进后出”的容器.      stack是简单地装饰deque容器而成为另外的一种容器.      #include <s ...

  2. 浅谈C++ STL stack 容器

    浅谈C++ STL stack 容器 本篇随笔简单介绍一下\(C++STL\)中\(stack\)容器的使用方法和常见的使用技巧. stack容器的概念 \(stack\)在英文中是栈的意思.栈是一种 ...

  3. C++ 标准模板库(STL)-stack

    主要介绍一下C++11版本中标准模板库中栈的用法,希望可以帮到需要用的人. #include <iostream> #include <stack> #include < ...

  4. C++ STL stack和queue

    C++ STL中独立的序列式容器只有vector,list,deque三种,stack和queue其实就是使用容器适配器对deque进行了封装,使用了新接口. 使用标准库的栈和队列时,先包含相关的头文 ...

  5. C++ STL stack 用法

    Stack(栈)是一种后进先出的数据结构,也就是LIFO(last in first out) ,最后加入栈的元素将最先被取出来,在栈的同一端进行数据的插入与取出,这一段叫做“栈顶”. 使用STL的s ...

  6. <泛> STL - stack 模拟实现

    今天,看C++Template的时候看到那人写了一个Stack,于是乎,手痒,自己也写了一个,在拜读了STD文件和C++模板元编程某些小节之后,你们就看到了这篇代码. 经过上述一番经历之后,我重新写了 ...

  7. 洛谷 P1739 表达式括号匹配【STL/stack/模拟】

    题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@"作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返 ...

  8. STL——stack

    首先,堆栈是一个线性表,插入和删除只在表的一端进行.这一端称为栈顶(Stack Top),另一端则为栈底(Stack Bottom).堆栈的元素插入称为入栈,元素的删除称为出栈.由于元素的入栈和出栈总 ...

  9. C++ STL——stack和queue

    目录 一 stack容器 二 queue容器 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 栈和队列作为经典的数据结构,我们再熟悉不过了.C++ ST ...

  10. STL stack 常见用法详解

    <算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...

随机推荐

  1. Intel+Ardruino 101

    为了传说中的那啥, 啊, 嗯.. #include <CurieBLE.h>const int ledPin = 13; // set ledPin to on-board LED  LE ...

  2. STM32的晶振跟HSE外部时钟设置.

    void RCC_Configuration(void){  /* RCC system reset(for debug purpose) */  RCC_DeInit(); /* Enable HS ...

  3. linux curl用法详解

    linux ‍‍curl用法详解 ‍‍curl的应用方式,一是可以直接通过命令行工具,另一种是利用libcurl库做上层的开发.本篇主要总结一下命令行工具的http相关的应用, 尤其是http下载方面 ...

  4. Android 标签的主题样式

    Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式 •andro ...

  5. href="#"会导致location.replace(location.href);脚本不工作

    我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...

  6. JavaEE基础(十四) /正则

    1.常见对象(正则表达式的概述和简单使用) A:正则表达式 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. 作用:比如注册邮箱,邮箱有用户名和 ...

  7. html+css复习之第1篇

    1. 保证在火狐浏览器字体<12px,苹果横屏的时候字体显示大小还是12px html { background: #fff; -webkit-text-size-adjust: 100%; - ...

  8. 转 python range 用法

    详细记录python的range()函数用法   使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的 ...

  9. Unity中HDR外发光的使用

    1.使用标准材质,设置好Emission外发光 2.Camera下打开HDR,加上Bloom特效 最终效果 如果只勾选HDR,只会有高光感,不会有外发光 注意,正向光照下打开HDR不可用抗锯齿,否则切 ...

  10. 第十四章:高级I/O

    14.1:引言 本章内容包括非阻塞I/O.记录锁.系统V流机制.I/O多路转接(select和poll函数).readv和writev函数以及存储映射I/O(mmap),这些都称为高级I/O. 14. ...