Imagine you have a vector of integers and you want to remove all zeros. It sounds like a simple task, right? Well, before C++20, it wasn’t that obvious. Let’s see how C++20 makes things easier.
Before
Since the beginning of C++, there is a free function named std::remove(). Young padawans may naively use it like this:
#include <iostream>
#include <vector>
int main() {
std::vector data{12, 0, 654, 0, 4587, 0, 42};
for (const auto& e: data) {
std::cout << e << '\t';
}
std::cout << '\n';
std::remove(data.begin(), data.end(), 0);
for (const auto& e: data) {
std::cout << e << '\t';
}
}
They would get this weird result: