Removing specific items from containers
Copying, transforming, and filtering are perhaps the most common operations on ranges of data. In this section, we concentrate on filtering items.
Filtering items out of data structures, or simply removing specific ones, works completely differently for different data structures. In linked lists (such as std::list
), for example, a node can be removed by making its predecessor point to its successor. After a node is removed from the link chain in this way, it can be given back to the allocator. In contiguously storing data structures (std::vector
, std::array
, and, to some extent, std::deque
), items can only be removed by overwriting them with other items. If an item slot is marked to be removed, all the items that are behind it must be moved one slot further to the front in order to fill the gap. This sounds like a lot of hassle, but if we want to simply remove whitespace from a string, for example, this should be achievable without much code.
When having...