How to Remove Negative Numbers From C++ Containers
summary
Shows concrete recipes: use erase_if vs remove_if + erase for vectors, use ranges::views::filter and views::keys/values for maps, write templates constrained by concepts to accept numeric types, and prefer std::strong_order or isnan handling for doubles because NaNs break comparisons. Notes arrays can’t shrink so you must copy or transform, explains nth_element/partition for fewer moves, warns about signed/unsigned promotion traps and offers a randomized sampling and a recursive filter as experimental alternatives.
AI-written summary · about
chapters (21)
- 0:00Introduction and motivation
- 2:58Basic removal with initializer list
- 4:22Erase if vs remove if
- 6:30Generalizing with templates
- 9:51Testing and constraints
- 14:32Handling other containers
- 16:17Arrays and fixed-size containers
- 18:03Working with maps
- 20:33Ranges and views
- 23:49Silly approaches
- 25:14Removing everything
- 26:16Shifting values to zero
- 28:26Counting and sorting negatives
- 30:16Using nth element
- 31:45Partitioning containers
- 34:35Recursive removal
- 37:28Bogo sort inspired removal
- 41:07Handling NaN values
- 44:04Signed vs unsigned comparisons
- 46:13Summary and best practices
- 49:26Q&A and closing remarks
