头文件 #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. Time of Trial

    Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...

  2. 一次修复linux的efi引导的集中方法总结记录

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...

  3. css笔记 - column分栏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Java获取两个指定日期之间的所有月份

    String y1 = "2016-02";// 开始时间 String y2 = "2019-12";// 结束时间 try { Date startDate ...

  5. 【Java 8】巧用Optional之优雅规避NPE问题

    避之不及的 NullPointerException NPE : NullPointerException 空指针异常是最常见的Java异常之一,抛出NPE错误不是用户操作的错误,而是开发人员的错误, ...

  6. magento2重写virtualType并且传参

    今天遇到一个需求需要重写一个block,但是这个block是应用virtualType实现,所以需要先重写virtualType,然后却因为参数丢失而获取不到正确的结果.因此,查阅文档,需要用type ...

  7. 剑指offer:和为S的两个数字

    题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 思 ...

  8. v-if和v-for一起使用的几个方法

    方法一(推荐): 不带if <ul> <li v-for="(item, index) in list" :key="index" > ...

  9. xadmin插件

    from django.http import HttpResponse from xadmin.plugins.actions import BaseActionView class test(Ba ...

  10. 调用百度API接口 正解析地址和逆解析

    地址解析(结构化地址 解析得到 经纬度): public void SaveLocation(DataRequest<Location> request, DataResponse< ...