より転載https://www.cnblogs.com/7z7chn/p/6341453.html
C ++、string
特定の文字をすべて削除し、次のコードを使用することができます
str.erase(STD ::削除(str.begin()、str.end()、' A ')、str.end());
これは、 remove
から<algorithm>
その署名であります
テンプレート< クラス ForwardIterator、クラス T> ForwardIterator削除(ForwardIterator最初、最後ForwardIterator、CONST T&ヴァル)。
役割:コンテナで、削除[first, last)
の間のすべての値が等しいval
値。
削除方法:等しいval
次の値とは等しくないval
カバレッジの値。
戻り値:反復子(表記newEnd
新しい容器に相当します)、最後の要素の次の要素へのイテレータが削除されていません、end
。
だから、完了するまで実行remove
、コンテナ[first, newEnd)
その内の要素は、すべての要素を削除されていない [newEnd, end)
要素の間には無用となっています。
このように、その後、実行str.erase(newEnd, str.end())
空にする[newEnd, end)
間の要素を。
std::remove_if
そしてremove
、それは3番目の引数が関数である受け入れることを除いて、類似しました。
// 例remove_if の#include <入出力ストリーム> // はstd :: COUT の#include <アルゴリズム> // はstd :: remove_if BOOL ISODD(int型 I){ リターン((I%2)== 1 )。} int型のmain(){ int型 myints [] = { 1、2、3、4、5、6、7、8、9 }。 // 1 2 3 4 5 6 7 8 9つの // 範囲の境界: INT* PBEGIN = myints。 // ^ int型 *保留= myints + はsizeof(myints)/ はsizeof(int型); // ^ ^ 保留 =のstd :: remove_if(PBEGIN、保留、ISODD)。 // 2 4 6 8????? // ^ ^ のstd :: coutのは、<< " の範囲は含まれています:" ; 以下のための(int型!;; P =保留++ * P = PBEGIN P) はstd :: coutの << ' ' << * P; std :: coutの <<' \ nを' ; リターン 0 ; }
出力:
myvectorは含まれています:10 30 30 10 10 0 0 0
あるstd::remove_copy
署名は:
テンプレート< クラス InputIterator、クラス OutputIterator、クラス T> OutputIterator remove_copy(InputIterator最初、最後InputIterator、 OutputIterator結果、CONST T&ヴァル)。
それはなります[first, last)
間で等しくないval
から要素をresult
コピー開始のコンテナ。
// remove_copy例 の#include <入出力ストリーム> // はstd :: COUT の#include <アルゴリズム> // はstd :: remove_copy の#include <ベクトル> // のstd ::ベクトル INT (){主 INT myints [] = { 10、20、30、30、20、10、10、20 }。 // 10 20 30 30 20 10 10 20 のstd ::ベクトル< 整数 > myvector(8 )。 std :: remove_copy(myints、myints + 8、myvector.begin()、20)。// 10 30 30 10 10 0 0 0 のstd :: coutの << " myvectorは含まれています:" ; 以下のため(のstd ::ベクトル< int型 ;それ= myvector.end()!++> ::それはmyvector.beginを()=イテレータそれ) はstd :: coutの << ' ' << * それ。 std :: coutの << ' \ nを' ; リターン 0 ; }
出力:
myvectorは含まれています:10 30 30 10 10 0 0 0