Good morning. I have a problem regarding iterators and custom classes. I confess that I am not so confident with them and I am finding difficult to set in mind how to create a custom operator. I know theory but practice is quite different. Can you please show me how could I create a simple custom operator to overload this list<Item> ? Many thanks!
To iterate over a const container you need a const_iterator. You also need to use typename to tell the compiler that const_iterator is actually a type.
1 2 3 4
for (typename list<Item>::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << endl;
}
EDIT: You don't need to use typename here because Item is not a template parameter.
Or you could let the compiler deduce the type of the iterator automatically by using the auto keyword.
1 2 3 4
for (auto it = l.begin(); it != l.end(); ++it)
{
cout << *it << endl;
}
Another possibility is to use range-based for loops and you don't have to have the iterator variable at all.