2013-11-09 226 views
0

我有下面的代碼,我想打印數組中的每個元素。C++打印數組列表的元素

struct pckt 
{ 
    float gen_time; 
    int node_id; 
    bool last; 
    int seq; 
    float end_time; 
} 

list<pckt> nodelist[51]; 

pckt newpckt; 
newpckt.gen_time = inp; 
newpckt.node_id = i; 
newpckt.last = false; 
newpckt.seq = 1; 
newpckt.end_time = 1.0; 

nodelist[i].push_back(newpckt); 

// I wnat to print each element in array list. 
+6

你嘗試過什麼? – Jon

回答

3

您沒有一個列表。你有一個包含51個列表元素的數組。 因此,要打印那些您需要遍歷數組並打印列表元素。
E.g:

for(int i=0; i < 51; ++i) 
{ 
    std::for_each(nodelist[i].begin(), nodelist[i].end(), 
     [](const pckt& e){ 
      std::cout << e.node_id << std::endl; 
     }); 
}