| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2139 人关注过本帖
标题:答案是这么写的,不能编译,不知道怎么改
只看楼主 加入收藏
不懂CCC
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
答案是这么写的,不能编译,不知道怎么改
#include <string>
#include <iostream>
#include <vector>

using namespace std;

typedef string elemtype;
class stack{
    public:
        virtual ~stack(){}
        virtual bool pop (elemtype& )=0;
        virtual bool push(const elemtype& )=0;
        virtual bool peek(int index,elemtype&)=0;
        
};

ostream& operator<<(ostream &os,const stack &rhs)
  {rhs.print();return os;}
   
class lifo_stack : public stack
{
    public:
        lifo_stack (int capacity=0):_top(0)
        {if(capacity) _stack.reserve(capacity);}
        int size() const{return _stack.size();}
        bool empty() const {return ! _top;}
        bool full() const {return size() >= _stack.max_size();}
        int top() const (return _top;)
        void print (ostream &os=cout) const;
        
        bool pop(elemtype &elem);
        bool push(const elemtype &elem);
        bool peek(int,elemtype&){return false;}
    private:
        vector<elemtype>_stack;
        int _top;
    };
   
    bool lifo_stack::pop(elemtype &elem){
        if (empty() ) return false;
        elem=_stack[--_top];
        _stack.pop_back();
        return true;
}
    bool lifo_stack::push(const elemtype &elem){
    if(full()) return false;
    _stack.push_back(elem);
    ++_top;
    return ture;
    }
    void lifo_stack::print(ostream &os) const{
      vector<elemtype>::const_reverse_iterator
            rit=_stack.rbegin(),
            rend=_stack.rend();
            
      os<<endl;
    }
   
    bool peekback_stack::
    peek (int index,elemtype &elem)
    {
        if(empty())
           return false;
        if (index<0|| index>=size())
         return false;
         elem=_stack[index];
         return true;
    }
    void peek(stack &st,int index)
    {
        cout <<endl;
        string t;
        if (st.peek(index,t))
           cout<<"peek:"<<t;
        else cout<<"peek failed!";
        cout <<endl;
    }
   
int main()
{
    lifo_stack st;
    string str;
    while (cin>>str &&!st.full())
           st.push(str);
           cout<<'\n'<<"about to call peek () whith lifo_stack"<<endl;
           peek(st,st.top()-1);
           cout<<st;
           
           peekback_stack pst;
           
           while (!st.empty()){
               string t;
               if (st.pop(t))
                 pst.push(t);
           }
    cout<<"about to call peek () with peekback_stack"<<endl;
    peek(pst,pst.top()-1);
    cout<<pst;
}
搜索更多相关主题的帖子: capacity include public return 
2015-05-20 12:56
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:20 
提示什么错误?

目测 main 内少了个return

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2015-05-20 22:21
快速回复:答案是这么写的,不能编译,不知道怎么改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018805 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved