我有2層地圖,其中第二層指向隊列。我想從隊列中提取一定數量的項目(因此也從地圖中提取),但是在正確的位置插入中斷/返回/退出(我不確定在這種情況下使用哪個項目)似乎有問題。在x個循環後退出while循環
這是代碼:
#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
謝謝,那工作! – sccs 2013-03-13 07:32:06