头文件 #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. 编译.tex文件时出现的问题

    Error: “the second byte of the cjk code is out of range” 发生在使用pdflatex和bibtex时.可能是.bib文档中出现了中文字符,包括中 ...

  2. Pyton项目打包成exe文件

    Python项目打包成exe文件 1 系统环境 windows版本: Win7 64位 python环境:Anaconda python版本:3.6 64位 pyinstaller版本:3.5 1 安 ...

  3. Swarm容器集群管理(超详细)

    一.Swarm介绍 Swarm是Docker公司自研发的容器集群管理系统, Swarm在早期是作为一个独立服务存在, 在Docker Engine v1.12中集成了Swarm的集群管理和编排功能.可 ...

  4. [代码质量] 推荐一个vs自带工具分析代码的复杂度

    转载自: https://blog.csdn.net/zh_geo/article/details/52954145 VS2012 -> Analyze -> Calculate code ...

  5. java中JSON转含泛型对象

    public static void main(String[] args) { UserDto userDto=new UserDto("test","14" ...

  6. Python 拼接字符串的几种方式

    在学习Python(3x)的过程中,在拼接字符串的时候遇到了些问题,所以抽点时间整理一下Python 拼接字符串的几种方式. 方式1,使用加号(+)连接,使用加号连接各个变量或者元素必须是字符串类型( ...

  7. MySQL导入csv文件内容到Table及数据库的自增主键设置

    写在前面 目的是测试将csv文件内容导入到表中, 同时记录一下自增主键的设置. 测试采用MySQL8.0. 新建表customer_info如下, 未设置主键. 修改上表, 添加主键id, 并设置为自 ...

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

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

  9. MySQL count

    https://www.jianshu.com/p/1b0a1f641e80] 不同引擎count(*)实现方式不同 MyISAM引擎把一个表的总行数记录在了磁盘上,执行count(*)会直接返回这个 ...

  10. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...