【问题标题】:Exiting while loop after x number of loopsx 次循环后退出 while 循环
【发布时间】:2013-03-01 00:05:59
【问题描述】:

我有两层地图,第二层指向一个队列。我想从队列(以及地图)中提取一定数量的项目,但在正确的位置插入中断/返回/退出(我不确定在这种情况下使用哪个)似乎有问题.

这是代码:

#include <iostream>
#include <queue>
#include <map>

using namespace std;

int main()
{
    typedef std::queue<int> MessageQueue;
    typedef std::map<int, MessageQueue> PriorityMap;
    typedef std::map<int, PriorityMap> ClientMap;

    ClientMap clients;

    int priorities = 7;

    clients[10][1].push(1);
    clients[10][1].push(2);
    clients[10][5].push(3);
    clients[10][7].push(3);

    for (int j = 1; j<3; j++) //this determines how many times an element will be 'popped' from a queue
    {
        while (!clients[10].empty())
        {
            for (int i = priorities; i > 0; i--)
            {
                while (clients[10].find(i) != clients[10].end())
                {
                    cout << "priority " << i << endl;
                    cout << clients[10][i].front() << endl;
                    clients[10][i].pop();

                    if (clients[10][i].empty())
                        clients[10].erase(i);

                }

            }

            break; //I don't know where this should go in order for the while loop to stop executing after an element has been popped
        }

    }

    return 0;
}

有了这个结果

priority 7
3
priority 5
3
priority 1
1
priority 1
2

但我想要的结果是

priority 7
3
priority 5
3

【问题讨论】:

    标签: c++ loops while-loop break


    【解决方案1】:

    main 方法的上下文中,exitreturn基本上会做同样的事情。都不建议在循环中间执行。

    我的建议:

    bool done = false;
    for (...; ... && !done; ...)
    {
       while (... && !done)
       {
          ...
          done = true;
          ...
       }
    }
    

    或者,根据您的问题:

    int count = 2;
    for (int j = 1; j<3 && count > 0; j++)
    {
       while (!clients[10].empty() && count > 0)
       {
          for (int i = priorities; i > 0 && count > 0; i--)
          {
             while (clients[10].find(i) != clients[10].end() && count > 0)
             {
                ...
                count--;
                ...
             }
          }
       }
    }
    

    我知道你可以只使用&amp;&amp; count 而不是&amp;&amp; count &gt; 0,但后者更具可读性。

    【讨论】:

      【解决方案2】:

      使用计数器和goto,而不是外部循环:

      int countRemoved = 0;
      while (!clients[10].empty())
      {
          for (int i = priorities; i > 0; i--)
          {
              while (clients[10].find(i) != clients[10].end())
              {
                  cout << "priority " << i << endl;
                  cout << clients[10][i].front() << endl;
                  clients[10][i].pop();
                  if (clients[10][i].empty())
                     clients[10].erase(i);
                  if( ++countRemoved == 2 ) goto stop;
             }
          }
      }
      stop:
      

      【讨论】:

      • 这似乎有点问题 - 如果我将 if( ++countRemoved == 2 ) goto stop; 行中的数字更改为比我在地图中拥有的总元素少一,它不会显示正确的元素数量。为什么会这样?
      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多