c ++打印数组列表的元素

时间:2013-11-09 13:24:16

标签: 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. 

1 个答案:

答案 0 :(得分:3)

您没有列表。你有一个包含列表的51个元素的数组到pcks。 因此,要打印那些需要迭代数组并打印列表元素的文件 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;
        });
}