If you have a recent compiler (one that includes at least a few C++11 features), you can avoid dealing with iterators (directly) if you want. For a list of "small" things like int
s, you might do something like this:
#include <list>
#include <iostream>
int main() {
list<int> mylist = {0, 1, 2, 3, 4};
for (auto v : mylist)
std::cout << v << "\n";
}
If the items in the list are larger (specifically, large enough that you'd prefer to avoid copying them), you'd want to use a reference instead of a value in the loop:
for (auto const &v : mylist)
std::cout << v << "\n";
` alone doesn't create any list. It only includes the relevant header file, but you still need to put code in place for creating, modifying, printing etc. the list. cppreference.com has example code for all kinds of standard containers, e.g. here for `std::list`: http://en.cppreference.com/w/cpp/container/list/push_back
– jogojapan Apr 26 '13 at 06:20