std::erase, std::erase_if (std::basic_string)
提供: cppreference.com
< cpp | string | basic string
ヘッダ <string> で定義
|
||
template< class CharT, class Traits, class Alloc, class U > constexpr typename std::basic_string<CharT,Traits,Alloc>::size_type |
(1) | (C++20以上) |
template< class CharT, class Traits, class Alloc, class Pred > constexpr typename std::basic_string<CharT,Traits,Alloc>::size_type |
(2) | (C++20以上) |
1)
value
と等しいすべての要素をコンテナから削除します。 以下と同等です。
auto it = std::remove(c.begin(), c.end(), value); auto r = std::distance(it, c.end()); c.erase(it, c.end()); return r;
2) 述語
pred
を満たすすべての要素をコンテナから削除します。 以下と同等です。
auto it = std::remove_if(c.begin(), c.end(), pred); auto r = std::distance(it, c.end()); c.erase(it, c.end()); return r;
目次 |
[編集] 引数
c | - | 削除元のコンテナ |
value | - | 削除する値 |
pred | - | 要素を削除するべき場合に true を返す単項述語。 式 pred(v) は |
[編集] 戻り値
削除した要素の数。
[編集] 計算量
線形。
[編集] 例
Run this code
#include <iostream> #include <numeric> #include <string> void print_container(const std::string& c) { for (auto x : c) { std::cout << x << ' '; } std::cout << '\n'; } int main() { std::string cnt(10, ' '); std::iota(cnt.begin(), cnt.end(), '0'); std::cout << "Init:\n"; print_container(cnt); std::erase(cnt, '3'); std::cout << "Erase \'3\':\n"; print_container(cnt); auto erased = std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; }); std::cout << "Erase all even numbers:\n"; print_container(cnt); std::cout << "In all " << erased << " even numbers were erased.\n"; }
出力:
Init: 0 1 2 3 4 5 6 7 8 9 Erase '3': 0 1 2 4 5 6 7 8 9 Erase all even numbers: 1 3 7 9 In all 5 even numbers were erased.
[編集] 関連項目
一定の基準を満たす要素を削除します (関数テンプレート) |