stack操作

Stack.h

#ifndef STACK_H_

#define STACK_H_

class Stack

{

public:

bool find(const string &elem) const;//查找某值

int count(const string &elem) const;//计算次数

bool push(const string&);

bool pop( string &elem);

bool peek(string &elem);

bool empty() const{ return _stack.empty();}

bool full() const{return _stack.size() == _stack.max_size();}

int size() const { return _stack.size();}

private:

vector<string> _stack;

};

#endif

Stack.cpp

#include<string>

#include<vector>

#include "Stack.h"

using namespace std;

bool Stack::pop(string &elem)

{

if(empty())

return false;

elem=_stack.back();

_stack.pop_back();

return true;

}

bool Stack::peek(string &elem)

{

if(empty())

return false;

elem=_stack.back();

return true;

}

bool Stack::push(const string &elem)

{

if(full())

return false;

_stack.push_back(elem);

return true;

}

bool Stack::find(const string &elem) const

{

vector<string>::const_iterator end_it = _stack.end();

return::find(_stack.begin(),end_it,elem)!=end_it;

}

int Stack::count(const string &elem) const

{

return::count(_stack.begin(),_stack.end(),elem);

}

main.cpp

#include<iostream>

#include<string>

#include<vector>

#include "Stack.h"

using namespace std;

int main()

{

Stack st;

string str;

while (cin>>str && !st.full())

{

st.push(str);

}

if (st.empty())

{

cout<<‘\n‘<<"Oops: no strings were read--bailing out\n";

return 0;

}

st.peek(str);

if (st.size()==1 && str.empty())

{

cout<<‘\n‘<<"Oops: no strings were read--bailing out\n";

return 0;

}

cout<<‘\n‘<<"Read in"<<st.size()<<"strings!\n"

<<"The strings,in reverse order:\n";

while (st.size())

{

if(st.pop(str))

cout<<str<<‘ ‘;

}

cout<<‘\n‘<<"There are now "<<st.size()<<"elements in the stack!\n";

cout<<‘\n‘<<"Read in"<<st.size()<<"strings!\n";

cin.clear();

cout<<"what word to search for?";

cin>>str;

bool found = st.find(str);

int count = found ? st.count(str) : 0;

cout<<str<<(found?"is":"isn‘t")<<"in the stack.";

if(found)

{

cout<<"It occurs"<<count<<"times\n";

}

}

时间: 2024-07-29 19:20:59

stack操作的相关文章

Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战

王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2 技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群 DT大数据梦工厂① :462923555 DT大数据梦工厂②:437123764 DT大数据梦工厂③

Stack操作,栈的操作。

栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法: boolean empty()           测试堆栈是否为空.  E peek() 查看堆栈顶部的对象,但不从堆栈中移除它.  E pop() 移除堆栈顶部的对象,并作为此函数的值返回该对象.  E push(E item) 把项压入堆栈顶部.  int search(Object o) 返回对象

string stack操作要注重细节问题

A string S consisting of N characters is considered to be properly nested if any of the following conditions is true: S is empty; S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string; S has the form &quo

请使劲回答一个关于UNIX/Linux自动扩展stack的问题

有本事就出来,没本事就当鳖! 如果让我回答关于进程栈,线程栈的问题,只要问题不笼统,只要问题明确,我会一 五一十地回答,正确率上九成,然而,可悲的是,问题往往他妈的都不是那么明确,因此,游戏到此结束!!艹.但是如果给我一个反问的机会,我会问提问者反问 下面一个问题,记住,使出你拉屎的劲来回答: UNIX/Linux 的stack在大多数平台是向下扩展的(注意,我已经告诉他事实了,我并没有问...是如何扩展的,这是可以背诵下来并朗读出来的),在一个执行流调用了 一个函数A,而该函数A在stack上

LeetCode——Min Stack

Description: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve

STL-vector,stack,list,queue

vector是可以快速地在最后添加删除元素,并可以快速地访问任意元素list是可以快速地在所有地方添加删除元素,但是只能快速地访问最开始与最后的元素deque在开始和最后添加元素都一样快,并提供了随机访问方法,像vector一样使用[]访问任意元素,但是随机访问速度比不上vector快,因为它要内部处理堆跳转deque也有保留空间.另外,由于deque不要求连续空间,所以可以保存的元素比vector更大,这点也要注意一下.还有就是在前面和后面添加元素时都不需要移动其它块的元素,所以性能也很高.

stl容器学习——queue,stack,list与string

目录 string stack queue list 点击上面的内容可以实现跳转哦 简介:本文记录了对string list queue stack四个容器的学习总结,包含有四种容器常用的函数介绍和一些使用过程中碰到的细节总结,在list容器中介绍了迭代器的使用. 头文件 想要用哪一种容器,就要加上对应的头文件 比如想要使用queue , 就要加上 #include<queue> 以此类推,想要使用STL库中的容器,只要加上它们的头文件就好. string 目录部分 1.string的定义及初

java求职宝典

浮点数 (1)float型数据定义必须加f后缀,否则编译不通过.例: float f = 3.53457f; (2)浮点数计算 System.out.println(( 3 - 2.6 == 0.4)); 输出flase 运算符优先级 第一级:() [] . ++(后置) --(后置) 第二级:++(前置) --(前置) +(一元加) -(一元减) !(一元逻辑非) 第三级:(type) new 第四级:* / % 第五级:+ - 第六级:<< >> >>> 第七

STL+位运算的文件

1.queue 队列 queue的头文件是<queue>. 定义queue对象的示例代码如: queue<int>q;  队列内存放的是int类型的数 queue<double> 队列内存放的是double类型的数 queue<node>q;  队列内存放的是结构体类型 入队列:q.push(x)   将x元素放到队列的末端. 出队列:q.pop()    将第一个元素删除 访问队首元素: q.front(); 访问队中的元素的个数: q.size(); 2