Stack(last in first out) and Exception
来源:互联网 发布:js根据id获取对象 编辑:程序博客网 时间:2024/06/05 23:55
1、 T& top(); const T& top() const;
数据结构讲到栈的实现函数其中public函数中有
T const& top (void)const {return data_[top_-1];}
和T& top (void) {return data_[top_-1];}
同样也有T const& operator[](int h)const {return data_[h];}
和T& operator[](int h) {return data_[h];}
T const&与T &的区别:
一个加了常量限定符,一个没加,也相应的一个是常引用,不能修改其值,一个为引用,可以修改值。
如果返回值是常引用,说明不能把返回值作为左值,也就是
a.top() = 10;//如果a 为常对象,这句是非法的
如果返回值是引用,就可以作为左值:
a.top() = 10; //如果a 不是常对象,这句把a的data_[top_-1]修改为10了;
常对象就是定义类对象时加上const限定符。
const CA a;
常对象只能调用常成员函数,也就是在函数未尾加了const限定符的成员函数,而在常成员函数里不能修改类的数据成员。
2、c++ STL 栈stack介绍
C++ Stack是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
c++ STL栈stack的头文件为:
#include<stack>
c++ STL栈stack的成员函数介绍:
操作 比较和分配堆栈empty() 堆栈为空则返回真pop() 移除栈顶元素push() 在栈顶增加元素size() 返回栈中元素数目top() 返回栈顶元素
栈的push(), pop()序列具体实现:
template<class T>bool stack<T>::push(T value) { if((MAXSIZE-1)==head){ cout<<"栈已满"<<endl; return false; } s[++head]=value; return true; }
template<class T> bool stack<T>::pop() { if(-1==head){ cout<<"栈已空"<<endl; return false; } --head; return true; }
范例:
转载自http://www.2cto.com/kf/201303/198735.html#include <iostream> using namespace std; const int MAXSIZE=10; template<class T> class stack { public: stack(); ~stack(); bool push(T value); bool pop(); bool empty(); T top(); private: T s[MAXSIZE];//栈对应的元素 int head;//指向栈顶元素 }; template<class T> bool stack<T>::empty() { return head==-1?1:0; } template<class T> stack<T>::stack() { head=-1; } template<class T> stack<T>::~stack() { } template<class T> bool stack<T>::push(T value) { if((MAXSIZE-1)==head){ cout<<"栈已满"<<endl; return false; } s[++head]=value; return true; } template<class T> bool stack<T>::pop() { if(-1==head){ cout<<"栈已空"<<endl; return false; } --head; return true; } template<class T> T stack<T>::top() { if(-1==head) throw 1; else return s[head]; } bool Is_Pop(int *a,int *b,int len_a,int len_b) { if(len_a!=len_b) return false; stack<int> s; int i=1,j=1; while(j!=len_a){ //如果输入的b数组数字有问题 if(b[j]<1 || b[j]>=len_a) return false; //如果数组a全部入栈,那么依次出栈和b[j]比较 if(i==len_a){ while(!s.empty()){ if(s.top()!=b[j++]) return false; s.pop(); } return true; } if(a[i]<b[j]){ s.push(a[i++]); } else { s.push(a[i]); ++i; ++j; s.pop(); } } return true; } void main() { stack<int> s; int Push[]={0,1,2,3,4,5};//从Push[1]开始 int Pop[]={0,4,3,5,1,2};//从Pop[1]开始 cout<<Is_Pop(Push,Pop,sizeof(Push)/sizeof(int),sizeof(Pop)/sizeof(int)); system("pause"); }
0 0
- Stack(last in first out) and Exception
- 数据筛选first. and last.
- Remove first and last element
- First and Second Chance Exception
- A FIFO (First In First Out)
- First-chance exception in xxx.exe...异常
- First-chance exception in Cairo on exit
- First-chance exception at 0x7c812a6b in
- Heap and stack in java
- Heap and Stack in programming
- FIFO队列(First In First Out)和优先队列
- Reference and Exception in WebServices
- 路由器in and out解释
- Sign In and Sign Out
- Sign In and Sign Out
- Sign In and Sign Out
- Sign In and Sign Out
- Pseudoclasses :first-child and :last-child for IE6
- Android-Parcelable接口的使用
- 347. Top K Frequent Elements
- PHP后台get和post带参请求的封装
- Eclipse console返回null
- 知道这20个正则表达式 能让你少写1,000行代码
- Stack(last in first out) and Exception
- 虚拟局域网VLAN
- django框架从零开始_010_自动化测试
- Spring依赖注入与配合接口编程案例
- 2016 UESTC Training for Dynamic Programming C - 柱爷的下凡 预处理打表、背包问题
- 中国餐馆过程(Chinese Restaurant Process)
- Web app相关思考
- lombok工具jar
- android:使用Properties保存程序配置