名前空間
変種
操作

std::erase_if (std::flat_map)

From cppreference.com
 
 
 
 
ヘッダ <flat_map> で定義
template< class Key, class T, class Compare, class KeyContainer, class MappedContainer,

          class Pred >
std::flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
    erase_if( std::flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c,

              Pred pred );
(C++23から)

c から、述語 pred を満たすすべての要素を消去する。

述語 pred は、式 bool(pred(std::pair<const Key&, const T&>(e)))true の場合に満たされるとみなされます。ここで ec 内のいずれかの要素です。

Key および TMoveAssignable である必要があります。それ以外の場合は、動作は未定義です。

目次

[編集] パラメーター

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

[編集] 戻り値

削除された要素の数。

[編集] 計算量

述語 pred の適用回数はちょうど c.size() 回です。

例外

erase_if が例外をスローした場合、c は有効ですが未指定(空になる可能性もあります)の状態のままになります。

注釈

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

[編集]

#include <iostream>
#include <flat_map>
 
void println(auto rem, auto const& container)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; const auto& [key, value] : container)
        std::cout << sep << '{' << key << ", " << value << '}', *sep = ',';
    std::cout << "}\n";
}
 
int main()
{
    std::flat_map<int, char> data
    {
        {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'},
        {5, 'e'}, {4, 'f'}, {5, 'g'}, {5, 'g'},
    };
    println("Original:\n", data);
 
    const auto count = std::erase_if(data, [](const auto& item)
    {
        auto const& [key, value] = item;
        return (key & 1) == 1;
    });
 
    println("Erase items with odd keys:\n", data);
    std::cout << count << " items removed.\n";
}

出力

Original:
{{1, a}, {2, b}, {3, c}, {4, d}, {5, e}}
Erase items with odd keys:
{{2, b}, {4, d}}
3 items removed.

[編集] 関連項目

特定の基準を満たす要素を削除する
(関数テンプレート) [編集]
特定の基準を満たす要素を削除する
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)