【问题标题】:Problem with throw exceptions when stack are empty ..... Queue / stack implementation堆栈为空时抛出异常的问题.....队列/堆栈实现
【发布时间】:2021-12-09 21:27:14
【问题描述】:

当两个堆栈都为空时我需要抛出一个异常,但我不知道该怎么写。

我必须实现一个有 2 个堆栈的队列!

这是主要的

#include "QueueFromStacks.h"

int main()
{
/*  THIS IS JUST TO SHOW YOU HOW #include <stack> WORKS
    stack<int> st1;
    stack<int> st2;
    cout << "Size before push:" << st2.size() << "\n";
    st2.push(2);
    st2.push(5);
    cout << "Size after two pushes:" << st2.size() << "\n";
    cout << st2.top() << "\n";
    st2.pop();
    cout << "Size of st2 after one pop:" << st2.size() << "\n";

    st1.push(st2.top());
    st2.pop();
    cout << "Size of st1:" <<st1.size()<< "   Size of st2:"<< st2.size();
*/
    QueueFromStacks<int> qfs;

    qfs.QueueFromStacks();

    qfs.enqueue(1);
    qfs.enqueue(2);
    qfs.enqueue(3);
    qfs.dequeue();
    cout  << "Queue Front : " << (qfs.front())<< endl;

// You have to implement QueuefromStack
// The logic of the queue remains the same(FIFO) but you have to use the two stacks to store your elements
// In the main program create a queuefromstack object and use your implemented methods to clearly show us what u did

    return 0;
}

头文件

    #ifndef QUEUEFROMSTACKS_H_
    #define QUEUEFROMSTACKS_H_
    
    #include <iostream>
    #include <stack>
    #include <string>
    
    using namespace std;
    
    class QueueEmptyException{
        public:
            QueueEmptyException();
            ~QueueEmptyException();
    
            string getMessage() { return "Queue is empty"; }
    };
    
    template <typename E>
    class QueueFromStacks
    {
        public:
            QueueFromStacks();
            ~QueueFromStacks();
    
            int size() const;
            bool empty() const;
            const E& front() const throw(QueueEmptyException);
            void enqueue (const E& e);
            void dequeue() throw(QueueEmptyException);
        private:
            stack<E> st1;
            stack<E> st2;
            int numElements;
    };
    #endif /* QUEUEFROMSTACKS_H_ */

实施

#include "QueueFromStacks.h"

template <typename E>
QueueFromStacks<E>::QueueFromStacks()
{
    numElements = 0;

}

template <typename E>
QueueFromStacks<E>::~QueueFromStacks()
{
    // TODO Auto-generated destructor stub
}

template <typename E>
int QueueFromStacks<E>::size() const
{
    return numElements;
}

template <typename E>
bool  QueueFromStacks<E>::empty() const
{
    return (size() == 0);
}

template <typename E>
const E& QueueFromStacks<E>::front() const
throw(QueueEmptyException)
{
    return st2.top();  // don't forget to check for empty and throw exception if it is empty.
}

template <typename E>
void QueueFromStacks<E>::enqueue (const E& e)
{
    st2.push(e);
    numElements++;
}

template <typename E>
void QueueFromStacks<E>::dequeue()
throw(QueueEmptyException)
{
    **// if both stacks are empty   // here i dont know what should i put as a throw condition  
    if (st1.empty() && st2.empty()) 
    {
        throw;
    }**

    // if the second stack is empty, move elements from the first stack to it
    if (st2.empty())
    {
        while (!st1.empty())
        {
            st2.push(st1.top());
            st1.pop();
        }
        // or make a call to swap(s1, s2)
    }

    // return the top item from the second stack
    int top = st2.top();
    st2.pop();
    numElements--;
}

【问题讨论】:

  • Exception specifiers like throw(QueueEmptyException) 在 C++11 中被弃用(超过 10 年)并在 C++17 中被删除。不应使用它们。
  • throw; 中,您忘记指定要抛出的异常类型。没有类型的 throw; 只能在 catch 块的上下文中工作,它会重新引发当前异常。你可能想要throw QueueEmptyException{};
  • stack&lt;E&gt; st1; 的目的是什么?没有代码向该容器添加任何内容。它总是空的。
  • QueueFromStacks&lt;E&gt;::~QueueFromStacks(){ // TODO Auto-generated destructor stub} -- 不再建议生成或编写空的析构函数调用。要么将析构函数声明为= default;,要么根本不实现它。通过编写一个空的 do-noteing 析构函数,类的特征变为非平凡可破坏的。这可能会导致编译器或其他模板化代码采用非有效的执行路径。

标签: c++ exception queue stack throw


【解决方案1】:

你需要改变这个:

void QueueFromStacks<E>::enqueue (const E& e)
{
    st2.push(e);
    numElements++;
}

到这里:

void QueueFromStacks<E>::enqueue (const E& e)
{
    st1.push(e);
    numElements++;
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2016-07-07
  • 2016-12-05
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2014-12-18
  • 2021-07-08
  • 2014-06-21
  • 1970-01-01
相关资源
最近更新 更多