C++

Erase–remove idiom (and std::erase_if)

Në shqip: Idioma erase–remove

ShpjegimiSQ

std::remove s'i fshin vërtet elementet — vetëm i zhvendos në fund. Duhet edhe erase: v.erase(std::remove(...), v.end()). Që nga C++20 mjafton std::erase_if(v, kushti).

EnglishEN

std::remove doesn't really delete items — it only moves them to the end. You also need erase: v.erase(std::remove(...), v.end()). Since C++20, std::erase_if(v, condition) is enough.

Si ta mendosh

Si t'i shtysh karriget e prishura në fund të sallës — duhet t'i nxjerrësh edhe jashtë.

Lexoje në anglisht

Like pushing broken chairs to the back of the hall — you still have to carry them out.

Shembull kodicpp

std::vector<int> v{5, 1, 4, 1};
v.erase(std::remove(v.begin(), v.end(), 1), v.end());   // stili i vjetër
std::erase_if(v, [](int x) { return x < 3; });          // C++20

Terma të lidhur