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
解决的迭代器第一个元素的位置将清除的大小。_Last
解决的迭代器通过最后一个元素的位置一将清除的大小。_It
解决的迭代器元素的位置将清除的字符串。_Pos
第一个字符的索引。要移除的字符串的。_Count
要移除的元素数,如果有许多在字符串范围内从 _Pos启动。
返回值
对于前两个成员函数,迭代器解决第一个字符放到成员函数中移除的最后一个字符之后。 为第三个成员函数,为元素清除的字符串对象的引用。
备注
第三个成员函数返回 *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