• <tfoot id='0zUQD'></tfoot>

      1. <small id='0zUQD'></small><noframes id='0zUQD'>

        <legend id='0zUQD'><style id='0zUQD'><dir id='0zUQD'><q id='0zUQD'></q></dir></style></legend>

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

      2. 为什么 std::queue::pop 不返回值.?

        Why doesn#39;t std::queue::pop return value.?(为什么 std::queue::pop 不返回值.?)

          <bdo id='3kEkq'></bdo><ul id='3kEkq'></ul>
          • <tfoot id='3kEkq'></tfoot>
              <tbody id='3kEkq'></tbody>

            <small id='3kEkq'></small><noframes id='3kEkq'>

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

                1. 本文介绍了为什么 std::queue::pop 不返回值.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我浏览了这个页面,但我无法获得道理一样.那里提到

                  I went through this page but I am not able to get the reason for the same . There it is mentioned that

                  "它根本不返回任何值并要求它更明智客户端使用 front() 检查队列前端的值"

                  "it is more sensible for it to return no value at all and to require clients to use front() to inspect the value at the front of the queue"

                  但是从 front() 检查一个元素也需要在左值中复制该元素.例如在这个代码段

                  But inspecting an element from front() also required that element to be copied in lvalue. For example in this code segment

                  std::queue<int> myqueue;
                  int myint;
                  int result;
                  std::cin >> myint;
                  myqueue.push (myint);
                  

                  /* 这里临时将在 RHS 上创建,并将分配给结果,以防万一如果通过引用返回,那么结果将在弹出操作后无效 */

                  /* here temporary will be created on RHS which will be assigned to result, and in case if returns by reference then result will be rendered invalid after pop operation */

                  result = myqueue.front();  //result.
                  std::cout << ' ' << result;
                  myqueue.pop();
                  

                  在第五行 cout 对象首先创建 myqueue.front() 的副本,然后将其分配给结果.那么,有什么区别,pop 函数也可以做同样的事情.

                  on fifth line cout object first creates a copy of myqueue.front() then assigns that to result. So, whats the difference, pop function could have done the same thing.

                  推荐答案

                  那么,有什么不同,pop 函数也可以做同样的事情.

                  So, whats the difference, pop function could have done the same thing.

                  它确实可以做同样的事情.之所以没有,是因为在存在异常(必须按值返回并因此创建副本)的情况下,返回弹出元素的 pop 是不安全的.

                  It could indeed have done the same thing. The reason it didn't, is because a pop that returned the popped element is unsafe in the presence of exceptions (having to return by value and thus creating a copy).

                  考虑这个场景(用一个天真的/虚构的 pop 实现来说明我的观点):

                  Consider this scenario (with a naive/made up pop implementation, to ilustrate my point):

                  template<class T>
                  class queue {
                      T* elements;
                      std::size_t top_position;
                      // stuff here
                      T pop()
                      {
                          auto x = elements[top_position];
                          // TODO: call destructor for elements[top_position] here
                          --top_position;  // alter queue state here
                          return x;        // calls T(const T&) which may throw
                      }
                  

                  如果 T 的复制构造函数在返回时抛出异常,则您已经更改了队列的状态(在我的幼稚实现中为 top_position)并且元素从队列中移除(并且未返回).出于所有意图和目的(无论您如何在客户端代码中捕获异常),队列顶部的元素都将丢失.

                  If the copy constructor of T throws on return, you have already altered the state of the queue (top_position in my naive implementation) and the element is removed from the queue (and not returned). For all intents and purposes (no matter how you catch the exception in client code) the element at the top of the queue is lost.

                  当您不需要弹出的值(即它创建了一个没人会使用的元素的副本)时,这种实现也是低效的.

                  This implementation is also inefficient in the case when you do not need the popped value (i.e. it creates a copy of the element that nobody will use).

                  这可以通过两个单独的操作(void popconst T& front())安全有效地实现.

                  This can be implemented safely and efficiently, with two separate operations (void pop and const T& front()).

                  这篇关于为什么 std::queue::pop 不返回值.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

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

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