Functions Versus Container Methods
Sometimes you have a choice between using an STL method and an STL function. Usually, the method is the better choice. First, it should be better optimized for a particular container. Second, being a member function, it can use a template class’s memory management facilities and resize a container when needed.
Suppose, for example, that you have a list of numbers and you want to remove all instances of a certain value, say 4
, from the list. If la
is a list<int>
object, you can use the list remove()
method:
la.remove(4); // remove all 4s from the list
After this method call, all elements with the value 4
are removed from the list, and the list is automatically resized.
There is also an STL algorithm called ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.