头文件 #include<stack>

stack<int>  s;

stack<char> s;//定义一个名字为s 的存int char的stack

基本指令

s.pop() 栈顶出栈。。

s.push(x) x入栈(无返回值的函数)

s.top()  访问栈顶元素

s.empty() 判断栈是否为空 是的话返回true

s.size() 计算栈中含有的元素。。while(s.size()) s.pop();多用来清空栈中的元素 好了 了解了基本用法 看一道题目。。
  hdu 1022

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
贴上代码#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
int main()
{
 int n,i,j;
 int flag[100];
 char in[100],out[100];
 while(scanf("%d %s %s",&n,&in,&out)!=EOF)
 {
  memset(flag,0,sizeof(flag));
  stack<char> s;
  while(s.size()) s.pop();
  i=j=0;
  for(i;i<=n;)
  {
   if(s.empty()||(!s.empty()&&s.top()!=out[j]))
   {
    s.push(in[i]);
    flag[i+j]++;
    i++;
      }
      if(s.top()==out[j])
      {
       s.pop();
       j++;
      }
  }
  if(s.empty())
  {
   cout<<"Yes."<<endl;
   for(i=0;i<n*2;i++)
   {
    if(flag[i]) cout<<"in"<<endl;
    else cout<<"out"<<endl;
   }
  
  }
  else cout<<"No."<<endl;
        cout<<"FINISH"<<endl;
  
 }
 return 0;
}
这是一道栈的入门题 要点有4个
1.如何判断入栈 出栈 
当栈为空的时候 或者栈中的pop不是需要的元素的时候 入
当pop是需要的 出
2.如何设置循环的终止条件
一个只有n个数 所以入栈的次数只有n次 每次入栈 i++
3.标记的应用      人栈的标记值为1    由于每次入栈 出栈对应的i J都会++ 所以I+J就是需要标记的数
4.如何判断条件成立 s.empty()

STL之 stack的基础应用的更多相关文章

  1. C++ STL编程轻松入门基础

    C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...

  2. STL之stack操作

    c++ stl栈stack介绍 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件 ...

  3. STL之stack(栈)

    栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...

  4. STL之stack

    一.stack(栈) 栈:LIFO 后进先出: 首先要指出的是,stack并非和STL的其他类模板是独立的容器,stack是自适应容器(容器适配器) stack<int, deque<in ...

  5. 带你深入理解STL之Stack和Queue

    上一篇博客,带你深入理解STL之Deque容器中详细介绍了deque容器的源码实现方式.结合前面介绍的两个容器vector和list,在使用的过程中,我们确实要知道在什么情况下需要选择恰当的容器来满足 ...

  6. ACM第二站————STL之stack

    栈,作为一种最基础的数据结构(栈还是一种内存的存储形式,就不介绍了),在各种数据结构的题目都会间接或者直接用到. 栈是一种受到限制的线性表,其限制是仅允许在表的一端进行插入和删除运算.这也给予了栈的一 ...

  7. C++ STL:stack和queue

    http://blog.csdn.net/wallwind/article/details/6858634 http://blog.csdn.net/chao_xun/article/details/ ...

  8. STL中stack/queue/map以及Boost unordered_map 的使用方法

    一.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...

  9. stl的stack在开发中的应用

    栈有后进先出特点,我们可以用它来暂时保存数据,在画板开发中,我用到了栈来保存用户的每一步操作,当用户点击撤销时可以把图像从栈里面取出,然后恢复.浏览器的前进和后退也是这个原理,只是它保存的是网页罢了. ...

随机推荐

  1. Kafka 消费者到底是什么 以及消费者位移主题到底是什么(Python 客户端 1.01 broker)

    Kafka 中有这样一个概念消费者组,所有我们去订阅 topic 和 topic 交互的一些操作我们都是通过消费者组去交互的. 在 consumer 端设置了消费者的名字之后,该客户端可以对多个 to ...

  2. 记录一次SpringBoot实现AOP编程

    需求 最近碰到一个问题,需要对关键操作的入参和返回值进行记录,并不是使用log记录,而是插入到数据库中. 思路:如果采用硬编码,在每个操作后都添加,会产生大量重复代码.因而打算使用自定义注解,通过AO ...

  3. mysql regexp 表达式

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  4. Android中如何动态添加碎片

    Android中的开发需要兼容手机和平板,两个方面.这就引入了碎片的概念.(注意:这里用的Fragment强烈建议使用support-v4库中的Fragment) 碎片:是一种可以嵌入在活动当中的UI ...

  5. Java的死锁及解决思路(延伸: 活锁,饥饿,无锁)

    死锁: A线程持有 锁1,接下来要获取锁2:与此同时,B线程持有锁2,要获取锁1.两个线程都在等对方释放自己需要的锁,这时两方会永远等待下去,就形成了死锁. 死锁的四个必要条件: 1.互斥:资源(锁) ...

  6. Android A/B System OTA分析(一)概览【转】

    本文转载自:https://blog.csdn.net/guyongqiangx/article/details/71334889 Android从7.0开始引入新的OTA升级方式,A/B Syste ...

  7. typescript - 6.泛型

    泛型类 class MinClas<T>{ public list:T[]=[]; add(value:T):void{ this.list.push(value); } min():T{ ...

  8. 通过SOCKS代理渗透整个内网

    https://blog.csdn.net/SouthWind0/article/details/83111044 通过SOCKS代理渗透整个内网 1.背景 经过前期的渗透工作,我们现在已经成功找到了 ...

  9. Spring cloud微服务安全实战-7-3prometheus环境搭建

    Prmetheus 主要用来做来Metrics的监控和报警,这张图是官方的架构图. 这是他的核心 它的作用是根据我们的配置去完成数据的采集.服务的发现,以及数据的存储. 这是服务的发现,通过Servi ...

  10. matlab学习笔记8 基本绘图命令-初级二维绘图/交互式绘图

    一起来学matlab-matlab学习笔记8 基本绘图命令_5 初级二维绘图/交互式绘图 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用&g ...