<small id='TaiKP'></small><noframes id='TaiKP'>

    1. <legend id='TaiKP'><style id='TaiKP'><dir id='TaiKP'><q id='TaiKP'></q></dir></style></legend>
        <bdo id='TaiKP'></bdo><ul id='TaiKP'></ul>
    2. <tfoot id='TaiKP'></tfoot>
    3. <i id='TaiKP'><tr id='TaiKP'><dt id='TaiKP'><q id='TaiKP'><span id='TaiKP'><b id='TaiKP'><form id='TaiKP'><ins id='TaiKP'></ins><ul id='TaiKP'></ul><sub id='TaiKP'></sub></form><legend id='TaiKP'></legend><bdo id='TaiKP'><pre id='TaiKP'><center id='TaiKP'></center></pre></bdo></b><th id='TaiKP'></th></span></q></dt></tr></i><div id='TaiKP'><tfoot id='TaiKP'></tfoot><dl id='TaiKP'><fieldset id='TaiKP'></fieldset></dl></div>
    4. 当堆栈为空时,'pop()' 方法应该返回什么?

      What should the #39;pop()#39; method return when the stack is empty?(当堆栈为空时,pop() 方法应该返回什么?)
    5. <tfoot id='M6rZg'></tfoot>

      • <bdo id='M6rZg'></bdo><ul id='M6rZg'></ul>
        <i id='M6rZg'><tr id='M6rZg'><dt id='M6rZg'><q id='M6rZg'><span id='M6rZg'><b id='M6rZg'><form id='M6rZg'><ins id='M6rZg'></ins><ul id='M6rZg'></ul><sub id='M6rZg'></sub></form><legend id='M6rZg'></legend><bdo id='M6rZg'><pre id='M6rZg'><center id='M6rZg'></center></pre></bdo></b><th id='M6rZg'></th></span></q></dt></tr></i><div id='M6rZg'><tfoot id='M6rZg'></tfoot><dl id='M6rZg'><fieldset id='M6rZg'></fieldset></dl></div>
        <legend id='M6rZg'><style id='M6rZg'><dir id='M6rZg'><q id='M6rZg'></q></dir></style></legend>

            <small id='M6rZg'></small><noframes id='M6rZg'>

                  <tbody id='M6rZg'></tbody>
              • 本文介绍了当堆栈为空时,'pop()' 方法应该返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                可能的重复:
                C++ STL栈问题:为什么栈为空pop()不抛出异常?

                在C++中设计堆栈时,当堆栈为空时pop()方法(或front()方法)应该返回什么?以下哪个设计更好?

                When designing a stack in C++, what should the pop() method (or front() method) return when the stack is empty? Which of the following design is better?

                1. 抛出异常
                2. 未定义,但要求用户在调用pop()
                3. 之前调用isempty()方法进行检查
                4. 返回一个布尔代码,同时使用一个额外的参数(一个引用)来传递弹出的元素
                5. 定义一个唯一的空元素
                1. Throw an exception
                2. Undefined, but require the user calling isempty() method to check before calling pop()
                3. Return a bool code, while using an extra parameter (a reference) to pass the popped element
                4. Define an unique empty element

                <小时>

                好吧,我看到我的问题不是那么清楚,让我尝试重写它:


                OK, I see that my question is not that clear, let me try to rewrite it:

                有一些数据结构可以基于链表来实现,比如栈、队列,它们每个都有一个返回前端元素(或尾部)的方法.

                There are some data structures which can be implemented based on linked list like stack, queue, and each of them has a methods returning the front element (or the tail).

                我想知道,对于数据为空的情况,有没有设计这种方法的原则指南.

                I want to know, is there any principle guideline about designing such a method regarding the case when the data is empty.

                我对更好的定义是容易正确使用,难以错误使用".

                And my definition of better is "easy to use correctly and hard to use incorrectly".

                推荐答案

                契约式编程风格是,非空堆栈是调用 pop<的先决条件/code>,并且在不满足其先决条件的情况下调用方法会产生未定义结果.我的实现会抛出一个std::logic_error,但这不是必需的.在 C 中,我的实现将通过 assertabort.

                The programming-by-contract style would be that having a non-empty stack is a precondition of calling pop, and that calling a method without meeting its preconditions has an undefined outcome. My implementation would throw a std::logic_error, but that would not be required. In C, my implementation would abort via assert.

                pop的调用者负责确保在调用pop之前栈不为空的前提条件成立.因此,堆栈应该有一个 isEmpty 方法供调用者检查.

                The caller of popis responsible for ensuring that the precondition that the stack is not empty holds before calling pop. The stack should therefore have an isEmpty method for the caller to check.

                这篇关于当堆栈为空时,'pop()' 方法应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                <small id='XlUdx'></small><noframes id='XlUdx'>

                <tfoot id='XlUdx'></tfoot>

                    <tbody id='XlUdx'></tbody>

                  1. <i id='XlUdx'><tr id='XlUdx'><dt id='XlUdx'><q id='XlUdx'><span id='XlUdx'><b id='XlUdx'><form id='XlUdx'><ins id='XlUdx'></ins><ul id='XlUdx'></ul><sub id='XlUdx'></sub></form><legend id='XlUdx'></legend><bdo id='XlUdx'><pre id='XlUdx'><center id='XlUdx'></center></pre></bdo></b><th id='XlUdx'></th></span></q></dt></tr></i><div id='XlUdx'><tfoot id='XlUdx'></tfoot><dl id='XlUdx'><fieldset id='XlUdx'></fieldset></dl></div>

                          <bdo id='XlUdx'></bdo><ul id='XlUdx'></ul>
                          <legend id='XlUdx'><style id='XlUdx'><dir id='XlUdx'><q id='XlUdx'></q></dir></style></legend>