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;
}
  

要求

标头:< 字符串>

命名空间: std

请参见

参考

basic_string 类