Template talk:cpp/container/erase

The given example doesn't work. It has two significant problems:

... should be:

... std::list is a template class, and requires a type.

Also, and this is the biggie, this doesn't work:

... because std::list supports bidirectional iterators, which don't support the + operator.

In order to delete from an offset on a bidirectional iterator, you need to iterate to the position, e.g.:

... and if you need to delete a range, you'll need to do this twice with two different iterators.

std::list is a linked list. The advantage is fast insert and delete. One disadvantage is that it doesn't support random access.

Bweinman 08:27, 1 April 2012 (PDT)


 * Now the example works with any kind of container but it's a bit ugly.
 * Maybe we should use for containers with random access iterators?
 * --Bazzy 10:38, 1 April 2012 (PDT)


 * That sounds like a great idea. Nate 12:08, 1 April 2012 (PDT)