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

    1. <small id='djTfb'></small><noframes id='djTfb'>

      1. 如何有效地清除 std::queue?

        时间:2023-10-07
          <legend id='koAm7'><style id='koAm7'><dir id='koAm7'><q id='koAm7'></q></dir></style></legend>

        • <tfoot id='koAm7'></tfoot>

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

            • <i id='koAm7'><tr id='koAm7'><dt id='koAm7'><q id='koAm7'><span id='koAm7'><b id='koAm7'><form id='koAm7'><ins id='koAm7'></ins><ul id='koAm7'></ul><sub id='koAm7'></sub></form><legend id='koAm7'></legend><bdo id='koAm7'><pre id='koAm7'><center id='koAm7'></center></pre></bdo></b><th id='koAm7'></th></span></q></dt></tr></i><div id='koAm7'><tfoot id='koAm7'></tfoot><dl id='koAm7'><fieldset id='koAm7'></fieldset></dl></div>
              • <bdo id='koAm7'></bdo><ul id='koAm7'></ul>
                    <tbody id='koAm7'></tbody>
                  本文介绍了如何有效地清除 std::queue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 std::queue 来实现 JobQueue 类.(基本上这个类以先进先出的方式处理每个作业).在一种情况下,我想一次性清除队列(从队列中删除所有作业).我在 std::queue 类中没有看到任何明确的方法.

                  I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner). In one scenario, I want to clear the queue in one shot( delete all jobs from the queue). I don't see any clear method available in std::queue class.

                  如何高效地实现 JobQueue 类的 clear 方法?

                  How do I efficiently implement the clear method for JobQueue class ?

                  我有一个简单的循环弹出解决方案,但我正在寻找更好的方法.

                  I have one simple solution of popping in a loop but I am looking for better ways.

                  //Clears the job queue
                  void JobQueue ::clearJobs()
                   {
                    // I want to avoid pop in a loop
                      while (!m_Queue.empty())
                      {
                          m_Queue.pop();
                      }
                  }
                  

                  推荐答案

                  用于清除标准容器的常见习惯用法是与空版本的容器交换:

                  A common idiom for clearing standard containers is swapping with an empty version of the container:

                  void clear( std::queue<int> &q )
                  {
                     std::queue<int> empty;
                     std::swap( q, empty );
                  }
                  

                  这也是实际清除某些容器(std::vector)中保存的内存的唯一方法

                  It is also the only way of actually clearing the memory held inside some containers (std::vector)

                  这篇关于如何有效地清除 std::queue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:错误:用“{...}"初始化;预期聚合对象 - C++ 下一篇:数据结构的“侵入性"是什么意思?

                  相关文章

                  最新文章

                      <bdo id='uhn1h'></bdo><ul id='uhn1h'></ul>
                  1. <small id='uhn1h'></small><noframes id='uhn1h'>

                    <legend id='uhn1h'><style id='uhn1h'><dir id='uhn1h'><q id='uhn1h'></q></dir></style></legend>

                      <tfoot id='uhn1h'></tfoot>

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