std::erase, std::erase_if (std::basic_string)
提供: cppreference.com
< cpp | string | basic string
| ヘッダ <string> で定義
|
||
| template< class CharT, class Traits, class Alloc, class U > void erase(std::basic_string<CharT,Traits,Alloc>& c, const U& value); |
(1) | (C++20以上) |
| template< class CharT, class Traits, class Alloc, class Pred > void erase_if(std::basic_string<CharT,Traits,Alloc>& c, Pred pred); |
(2) | (C++20以上) |
1) コンテナから
value と等しいすべての要素を削除します。 c.erase(std::remove(c.begin(), c.end(), value), c.end()); と同等です。2) コンテナから述語
pred を満たすすべての要素を削除します。 c.erase(std::remove_if(c.begin(), c.end(), pred), c.end()); と同等です。目次 |
[編集] 引数
| 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, '5'); std::cout << "Erase \'5\':\n"; print_container(cnt); std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; }); std::cout << "Erase all even numbers:\n"; print_container(cnt); }
出力:
Init: 0 1 2 3 4 5 6 7 8 9 Erase '5': 0 1 2 3 4 6 7 8 9 Erase all even numbers: 1 3 7 9
[編集] 関連項目
| 一定の基準を満たす要素を削除します (関数テンプレート) |