Template:cpp/experimental/erase if

Erases all elements that satisfy the predicate from the container.

Complexity
Linear.

Example
{{#switch:{{{1}}}

{{example |code=
 * map=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& cont) {   os << "{"; for (const auto& item : cont) { os << "{" << item.first << ", " << item.second << "}"; }   return os << "}"; }

int main {   std::map data {{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'}, {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}}; std::cout << "Original:\n" << data << '\n';

std::experimental::erase_if(data, [](const auto& item) {       return (item.first & 1) == 1;    });

std::cout << "Erase items with odd keys:\n" << data << '\n'; } |output= Original: {{1, a}{2, b}{3, c}{4, d}{5, e}} Erase items with odd keys: {{2, b}{4, d}} }}

{{example |code=
 * multimap=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& cont) {   os << "{"; for (const auto& item : cont) { os << "{" << item.first << ", " << item.second << "}"; }   return os << "}"; }

int main {   std::multimap data {{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'}, {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}}; std::cout << "Original:\n" << data << '\n';

std::experimental::erase_if(data, [](const auto& item) {       return (item.first & 1) == 1;    });

std::cout << "Erase items with odd keys:\n" << data << '\n'; } |output= Original: {{1, a}{2, b}{3, c}{4, d}{4, f}{5, e}{5, g}{5, g}} Erase items with odd keys: {{2, b}{4, d}{4, f}} }}

{{example |code=
 * unordered_map=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& cont) {   os << "{"; for (const auto& item : cont) { os << "{" << item.first << ", " << item.second << "}"; }   return os << "}"; }

int main {   std::unordered_map data {{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'}, {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}}; std::cout << "Original:\n" << data << '\n';

std::experimental::erase_if(data, [](const auto& item) {       return (item.first & 1) == 1;    });

std::cout << "Erase items with odd keys:\n" << data << '\n'; } |p=true |output= Original: {{5, e}{4, d}{3, c}{2, b}{1, a}} Erase items with odd keys: {{4, d}{2, b}} }}

{{example |code=
 * unordered_multimap=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& cont) {   os << "{"; for (const auto& item : cont) { os << "{" << item.first << ", " << item.second << "}"; }   return os << "}"; }

int main {   std::unordered_multimap data {{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'}, {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}}; std::cout << "Original:\n" << data << '\n';

std::experimental::erase_if(data, [](const auto& item) {       return (item.first & 1) == 1;    });

std::cout << "Erase items with odd keys:\n" << data << '\n'; } |p=true |output= Original: {{5, g}{5, g}{5, e}{4, f}{4, d}{3, c}{2, b}{1, a}} Erase items with odd keys: {{4, f}{4, d}{2, b}} }}

{{example |code=
 * set=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& container) {   os << "{ "; for (const auto& item : container) { os << item << ' '; }   return os << "}"; }

int main {   std::set data { 3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0 };

std::cout << "Original:\n" << data << '\n';

auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };

std::experimental::erase_if(data, divisible_by_3);

std::cout << "Erase all items divisible by 3:\n" << data << '\n'; } |output= Original: { 0 1 2 3 4 5 6 7 } Erase all items divisible by 3: { 1 2 4 5 7 } }}

{{example |code=
 * multiset=
 * 1) include 
 * 2) include

template inline Os& operator<<(Os& os, Container const& container) {   os << "{ "; for (const auto& item : container) { os << item << ' '; }   return os << "}"; }

int main {   std::multiset data { 3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0 };

std::cout << "Original:\n" << data << '\n';

auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };

std::experimental::erase_if(data, divisible_by_3);

std::cout << "Erase all items divisible by 3:\n" << data << '\n'; } |output= Original: { 0 1 2 3 3 4 5 5 6 6 7 } Erase all items divisible by 3: { 1 2 4 5 5 7 } }}

{{example |code=
 * unordered_set=
 * 1) include <experimental/unordered_set>
 * 2) include

template<typename Os, typename Container> inline Os& operator<<(Os& os, Container const& container) {   os << "{ "; for (const auto& item : container) { os << item << ' '; }   return os << "}"; }

int main {   std::unordered_set data { 3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0 };

std::cout << "Original:\n" << data << '\n';

auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };

std::experimental::erase_if(data, divisible_by_3);

std::cout << "Erase all items divisible by 3:\n" << data << '\n'; } |p=true |output= Original: { 0 1 2 7 6 5 4 3 } Erase all items divisible by 3: { 1 2 7 5 4 } }}

{{example |code=
 * unordered_multiset=
 * 1) include <experimental/unordered_set>
 * 2) include

template<typename Os, typename Container> inline Os& operator<<(Os& os, Container const& container) {   os << "{ "; for (const auto& item : container) { os << item << ' '; }   return os << "}"; }

int main {   std::unordered_multiset data { 3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0 };

std::cout << "Original:\n" << data << '\n';

auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };

std::experimental::erase_if(data, divisible_by_3);

std::cout << "Erase all items divisible by 3:\n" << data << '\n'; } |p=true |output= Original: { 0 1 2 7 6 6 5 5 4 3 3 } Erase all items divisible by 3: { 1 2 7 5 5 4 } }}

{{example |code=
 * basic_string=
 * 1) include <experimental/string>
 * 2) include
 * 3) include

int main {   std::string data { "1337!p_C00L_<a-_HACKER_!@s_{!s#@_w^o%r*d#42" };

std::cout << "Original string: " << quoted(data) << '\n';

auto crack = [](auto O) { return O < 0141 or 0172 < O; };

std::experimental::erase_if(data, crack);

std::cout << "After erase_if: " << quoted(data) << '\n'; } |output= Original string: "1337!p_C00L_<a-_HACKER_!@s_{!s#@_w^o%r*d#42" After erase_if: "password" }} |code=
 * {{example
 * 1) include <experimental/{{{1}}}>
 * 2) include

template<typename Os, typename Container> inline Os& operator<<(Os& os, Container const& container) {   os << "{ "; for (const auto& item : container) { os << item << ' '; }   return os << "}"; }

int main {   std::{{{1}}} data { 3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0 };

std::cout << "Original:\n" << data << '\n';

auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };

std::experimental::erase_if(data, divisible_by_3);

std::cout << "Erase all items divisible by 3:\n" << data << '\n'; } |output= Original: { 3 3 4 5 5 6 6 7 2 1 0 } Erase all items divisible by 3: { 4 5 5 7 2 1 } }} }}