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. linux与KVM虚拟里的windows实现文件共享

    1.把windows系统里的共享文件设置为共享 2.在linux系统里 mount -t cifs //192.168.0.254/work /data/tmp -o username=test,pa ...

  2. nginx+tomcat负载均衡+动静分离+redis集中管理session

    1.服务器A安装ng,服务器B.C安装tomcat: 2.服务器A建立/data/www目录,用于发布静态文件: 3.ng无动静分离配置: user root root; worker_process ...

  3. crontab实现每秒执行

    crontab: #!/bin/bash step=$1 #每多少秒执行一次 ; i < ; i=(i+step) )); do date +%Y%m%d' '%H:%M:%S >> ...

  4. iOS Button按钮 热区的放大

      Apple的iOS人机交互设计指南中指出,按钮点击热区应不小于44x44pt,否则这个按钮就会让用户觉得“很难用”,因为明明点击上去了,却没有任何响应. 但我们有时做自定义Button的时候,设计 ...

  5. Docker CPU 资源限制——CPU固定核功能测试

    Docker使用Linux cgroup来实现资源的限制,对于CPU的限制有两种方法: 1.cpuset CPU Set限定容器使用某个固定的CPU核.使用默认的libcontainer引擎时,可以通 ...

  6. PHP面向对象的一些深入理解

    1.$this就是这个对象的地址,$this不能在类外部使用.2.构造函数 __construct 和析构函数都没有返回值:一旦一个对象成为垃圾对象(没有任何变量引用的对象,或者=null),析构函数 ...

  7. postgresql 热备与恢复

    一. PostgreSQL热备份的过程一般为: 数据库中执行:pg_start_backup() ; 然后使用操作系统的tar或 cp命令拷贝 PostgreSQL数据文件. 数据库中执行:pg_st ...

  8. SQL Server 索引和表体系结构(二)

    转自:http://www.cnblogs.com/chenmh 非聚集索引 概述 对于非聚集索引,涉及的信息要比聚集索引更多一些,由于整个篇幅比较大涉及接下来的要写的“包含列的索引”,“索引碎片”等 ...

  9. Java中的ClassLoader

      Java中类的加载过程(如Dog类):   通过类型信息定位Dog.class文件. 载入Dog.class文件,创建相应的Class对象. 执行父类的静态字段定义时初始化语句和父类的静态初始化块 ...

  10. hdu5681 zxa and wifi

    这道题目是不太好想的,尽管很容易看出来应该是dp,但是状态转移总觉得无从下手. 其实我们可以将状态分层,比如设$dp(i, j)$为用$i$个路由器覆盖前$j$个点所需的最小代价 我们先不考虑状态,而 ...