【摘抄】A Conclusion Of Choosing Among Erasing Options

2007-8-29 Nie.Meining Coding

To eliminate all objects in a container that have a particular value:
If the container is a vector, string, or deque, use the erase-remove idiom.
If the container is a list, use list::remove.
If the container is a standard associative container, use its erase member function.

To eliminate all objects in a container that satisfy a particular predicate:
If the container is a vector, string, or deque, use the erase-remove_if idiom.
If the container is a list, use list::remove_if.
If the container is a standard associative container, use remove_copy_if and swap, or write a loop to walk the container elements, being sure to postincrement your iterator when you pass it to erase.

To do something inside the loop (in addition to erasing objects):
If the container is a standard sequence container, write a loop to walk the container elements, being sure to update your iterator with erase's return value each time yon call it.
If the container is a standard associative container, write a loop to walk the container elements, being sure to postincrement your iterator when you pass it to erase.

Taken from 《Effective STL》

发表评论:

Powered by emlog