You're close.
std::list<Student>::iterator it;
for (it = data.begin(); it != data.end(); ++it){
std::cout << it->name;
}
Note that you can define it
inside the for
loop:
for (std::list<Student>::iterator it = data.begin(); it != data.end(); ++it){
std::cout << it->name;
}
And if you are using C++11 then you can use a range-based for
loop instead:
for (auto const& i : data) {
std::cout << i.name;
}
Here auto
automatically deduces the correct type. You could have written Student const& i
instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…