Stack(last in first out) and Exception

本文介绍了数据结构中的栈及其C++实现,探讨了`T const&`与`T &`的区别,并详细阐述了C++ STL栈stack的功能和成员函数,包括push和pop操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   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"); 
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值