2015-01-26 129 views
-1

我創建了一個隊列向量。隊列中有10個向量和大約1000個元素。然後,我嘗試以循環方式從隊列中的所有向量中取出一個元素。但是我觀察到一個很奇怪的現象。我看到一些向量有兩個元素出列,有些沒有。出列時出現奇怪現象

struct pkt{ 
    int data; 
    int time; 
}; 

//Input queues are already filled with 1000 packets 
std::vector<std::queue<pkt>> per_port_input_queue; 
std::vector<std::queue<pkt>> per_port_output_queue; 

// Dequeing one element at a time in a round-robin fashion 
for(int i=0;i<5; i++) 
{ 
decoded_pkt=per_port_input_queue[i].front(); 
send_to_output(decoded_pkt); 
per_port_input_queue[decoded_pkt.data].pop(); 
} 

void send_to_output(pkt decoded_pkt) 
{ 
    per_port_output_queue[decoded_pkt.data].push(decoded_pkt); 

} 

下面你可以找到觀察到的輸出和我的預期輸出。

======================================================== 
Port  I/p (t=0) (after dequing  My 
          5 pkt)  Expected result 
======================================================== 
0   1000   1000    999 
1   1000   1000    999 
2   1000   1000    999 
3   1000   998    999 
4   1000   1000    999 
5   1000   1000    1000 
6   1000   999    1000 
7   1000   999    1000 
8   1000   1000    1000 
9   1000   999    1000 

有人可以澄清爲什麼會發生這種情況,以及如何避免這種情況。有沒有在C使用隊列++

回答

3
// Dequeing one element at a time in a round-robin fashion 
for(int i=0;i<5; i++) 
{ 
decoded_pkt=per_port_input_queue[i].front(); 
send_to_output(decoded_pkt); 
per_port_input_queue[decoded_pkt.data].pop(); <<<<<====== 
} 

一些問題殺出隊列取決於你從第i個隊列中讀取數據,所以它也就不足爲奇了。如果你的意思是閱讀,然後彈出相同的隊列你需要去

// Dequeing one element at a time in a round-robin fashion 
for(int i=0;i<5; i++) 
{ 
decoded_pkt=per_port_input_queue[i].front(); 
send_to_output(decoded_pkt); 
per_port_input_queue[i].pop(); 
} 
+0

謝謝,我接受你的答案。這是一個愚蠢的錯誤:-) – user2532296 2015-01-26 21:46:20

+0

我們都做到了 – pm100 2015-01-26 21:46:38

相關問題