キューの使用 (およびベクトルの実践) について学ぶために、簡単なコードをテストしています。
私はこのコードを書きました:
#include "stdafx.h"
#include <iostream>
#include <queue>
struct msgInfo //contains the attributes as gleaned from the original (IP) message
{
int age;
std::string name;
};
using namespace std;
int main ()
{
vector<vector<queue<msgInfo>>> nodeInc; //container for messages
int qosLevels = 7; //priority levels
int nodes = 5; //number of nodes
vector<queue<msgInfo>> queuesOfNodes(qosLevels);
int i;
for (i=0; i<nodes; i++)
{
nodeInc.push_back(queuesOfNodes);
}
msgInfo potato, tomato, domato, bomato;
potato.age = 2;
potato.name = "dud";
tomato.age = 3;
tomato.name = "bud";
domato.age = 4;
domato.name = "mud";
bomato.age = 5;
bomato.name = "pud";
nodeInc[2][2].push(potato);
nodeInc[2][2].push(tomato);
nodeInc[2][3].push(domato);
nodeInc[2][3].push(bomato);
for (int j = 0; j < 2; j++) //simple loop for testing: for each round, output the age of only one 'msgInfo'
{
cout << j << endl;
for (int k = (qosLevels-1); k >= 0; k--)
{
if (!nodeInc[2][k].empty())
{
cout << nodeInc[2][k].front().age << endl;
nodeInc[2][k].pop();
return 0;
}
else
break;
}
}
}
私が得る出力は
0
1
しかし、私が取得しようとしているのは
0
4
1
5
ここで何が間違っていますか?ロジックのどこが間違っているのかわかりません。ここでは、最高の優先度レベルに属する最初の 2 つの要素を出力する必要があるようです。ループを終了する方法に関係していると思います-基本的に、forループの各ラウンドで、「ポップ」する前に1つのmsgInfoの年齢のみを出力する必要があります-しかし、終了/リターン/ブレークを試しました動作していません。
編集
ノードからメッセージを受信しています。これらのメッセージは、属性 (ノードと優先度レベル) に従ってキューに入れる必要があります。これを行うために a を使用することvector<vector<queue<msgInfo>>>
にしました -> 基本的にノード < 優先度レベル < メッセージのキュー > >. このコンテナーにアクセスするとき、一度に 1 つの msgInfo の経過時間を出力する必要があります。msgInfo は、最高の優先度レベルのキューの先頭になります。すべての優先度レベルが満たされるわけではないため、関連する要素を見つけるために、最高の優先度レベルから最低の優先度レベルまで反復する必要があります。
これらを一度に出力するループを設計する必要があります (ループの各ラウンド間で他の処理を行う必要があるため)。