basic_string::erase
從移除指定位置處的一或多個項目範圍進行解碼的字串。
iterator erase(
iterator _First,
iterator _Last
);
iterator erase(
iterator _It
);
basic_string<CharType, Traits, Allocator>& erase(
size_type _Pos = 0,
size_type _Count = npos
);
參數
_First
處理第一個項目的Iterator的位置在即將擦掉的範圍。_Last
解決的Iterator超過最後一個項目的位置是在即將擦掉的範圍。_It
處理項目的Iterator的位置在即將擦掉的字串。_Pos
第一個字元的索引中移除的字串。_Count
要移除的項目數目,如果有許多在字串的範圍內 _Pos開始。
傳回值
在前兩個成員函式, Iterator處理第一個字元在成員函式來移除的最後一個字元之後。 針對第三+成成員函式,項目會清除之字串物件的參考。
備註
第三+成成員函式傳回 *this。
範例
// basic_string_erase.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// The 1st member function using a range demarcated
// by iterators
string str1 ( "Hello world" );
basic_string <char>::iterator str1_Iter;
cout << "The original string object str1 is: "
<< str1 << "." << endl;
str1_Iter = str1.erase ( str1.begin ( ) + 3 , str1.end ( ) - 1 );
cout << "The first element after those removed is: "
<< *str1_Iter << "." << endl;
cout << "The modified string object str1 is: " << str1
<< "." << endl << endl;
// The 2nd member function erasing a char pointed to
// by an iterator
string str2 ( "Hello World" );
basic_string <char>::iterator str2_Iter;
cout << "The original string object str2 is: " << str2
<< "." << endl;
str2_Iter = str2.erase ( str2.begin ( ) + 5 );
cout << "The first element after those removed is: "
<< *str2_Iter << "." << endl;
cout << "The modified string object str2 is: " << str2
<< "." << endl << endl;
// The 3rd member function erasing a number of chars
// after a char
string str3 ( "Hello computer" ), str3m;
basic_string <char>::iterator str3_Iter;
cout << "The original string object str3 is: "
<< str3 << "." << endl;
str3m = str3.erase ( 6 , 8 );
cout << "The modified string object str3m is: "
<< str3m << "." << endl;
}
需求
標題: <string>
命名空間: std