Namespaces
Variants

std:: erase_if (std::flat_set)

From cppreference.net

ヘッダーで定義 <flat_set>
template < class Key, class Compare, class KeyContainer,

class Pred >
std:: flat_set < Key, Compare, KeyContainer > :: size_type
erase_if ( std:: flat_set < Key, Compare, KeyContainer > & c,

Pred pred ) ;
(C++23以降)
(C++26以降 constexpr)

pred を満たすすべての要素を c から削除します。

述語 pred は、式 bool ( pred ( std:: as_const ( e ) ) ) true の場合に満たされます。ここで e c 内の任意の要素です。

Key MoveAssignable でない場合、動作は未定義です。

目次

パラメータ

c - 消去元のコンテナアダプタ
pred - 要素が消去されるべき場合に true を返す述語

戻り値

削除された要素の数。

計算量

述語 c. size ( ) の正確な回数だけ pred を適用します。

例外

erase_if が例外を送出した場合、 c は有効だが未指定(空の可能性あり)の状態を維持します。

注記

このアルゴリズムは安定しています。つまり、削除されない要素の順序は変更されません。

#include <iostream>
#include <flat_set>
void println(auto rem, const auto& container)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; const auto& item : container)
        std::cout << sep << item, *sep = ',';
    std::cout << "}\n";
}
int main()
{
    std::flat_set data{3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0};
    println("Original:\n", data);
    auto divisible_by_3 = [](const auto& x) { return (x % 3) == 0; };
    const auto count = std::erase_if(data, divisible_by_3);
    println("Erase all items divisible by 3:\n", data);
    std::cout << count << " items erased.\n";
}

出力:

Original:
{0, 1, 2, 3, 4, 5, 6, 7}
Erase all items divisible by 3:
{1, 2, 4, 5, 7}
3 items erased.

関連項目

特定の条件を満たす要素を削除する
(関数テンプレート)
特定の条件を満たす要素を削除する
(アルゴリズム関数オブジェクト)