std:: erase_if (std::flat_map)
|
ヘッダーで定義
<flat_map>
|
||
|
template
<
class
Key,
class
T,
class
Compare,
class
KeyContainer,
class
MappedContainer,
class
Pred
>
|
(C++23以降)
(C++26以降 constexpr) |
|
pred を満たすすべての要素を c から削除します。
述語 pred は、式 bool ( pred ( std:: pair < const Key & , const T & > ( e ) ) ) が true の場合に満たされます。ここで e は c 内の任意の要素です。
Key
または
T
が
MoveAssignable
でない場合、動作は未定義です。
目次 |
パラメータ
| c | - | 要素を削除するコンテナアダプタ |
| pred | - | 要素が削除されるべき場合に true を返す述語 |
戻り値
削除された要素の数。
計算量
述語 c. size ( ) の正確に pred の適用回数。
例外
erase_if
が例外を送出した場合、
c
は有効だが未指定(空の可能性もある)状態を維持します。
注記
このアルゴリズムは安定しています。つまり、削除されない要素の順序は変更されません。
例
#include <iostream> #include <flat_map> void println(auto rem, const auto& 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) { const auto& [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.
関連項目
|
特定の条件を満たす要素を削除する
(関数テンプレート) |
|
|
(C++20)
(C++20)
|
特定の条件を満たす要素を削除する
(アルゴリズム関数オブジェクト) |