标题：Erase
简述
描述[1]重新初始化固定大小数组的元素，并释放动态数组的存储空间。语法Erasearray
array 参数是要清除的数组变量的名称。
iterator erase( iterator pos ); 
iterator erase( iterator start, iterator end ); 
basic_string &erase( size_type index = 0, size_type num = npos ); 
删除pos指向的字符, 返回指向下一个字符的迭代器, 
删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置 
删除从index索引开始的num个字符, 返回*this. 
参数index 和 num 有默认值, 这意味着erase()可以这样调用：只带有index以删除index后的所有字符，或者不带有任何参数以删除所有字符.
说明判断数组是固定长度数组（常规）还是动态数组是很重要的，这是因为 Erase 要根据数组的类型进行不同的操作。Erase 无需为固定大小的数组还原内存。Erase 按照下表设置固定数组的元素：
数组的类型Erase 对固定数组元素的影响固定数值数组将每个元素设置为 0。固定字符串数组将每个元素设置为零长度字符串 ("")。对象数组将每个元素设置为特殊值 Nothing。Erase 释放动态数组所使用的内存。在程序再次引用该动态数组之前，必须使用 ReDim 语句来重新定义该数组变量的维数。
头文件
<string>
函数原型
iterator erase(
iterator _First, 
iterator _Last
);
iterator erase(
iterator _It
);
basic_string& erase(
size_type_Pos = 0,
size_type _Count = npos
);
参数
_First
An iterator addressing the position of the first element in the range to be erased.
_Last
An iterator addressing the position one past the last element in the range to be erased.
_It
An iterator addressing the position of the element in the string to be erased.
_Pos
The index of the first character in the string to be removed.
_Count
The number of elements that will be removed if there are as many in the range of the string beginning with _Pos.
返回值
For the first two member functions, an iterator addressing the first character after the last character removed by the member function. For the third member function, a reference to the string object from which the elements have been erased.
举例
例一：string s("So, you like donuts, eh? Well, have all the donuts in the world!"); 
cout << "The original string is '" << s << "'" << endl; 
s.erase( 50, 14 ); 
cout << "Now the string is '" << s << "'" << endl; 
s.erase( 24 ); 
cout << "Now the string is '" << s << "'" << endl; 
s.erase(); 
cout << "Now the string is '" << s << "'" << endl; 
将显示 
The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!' 
Now the string is 'So, you like donuts, eh? Well, have all the donuts' 
Now the string is 'So, you like donuts, eh?' 
Now the string is ''
例二：// 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;
return 0;
}
输出：
The original string object str1 is: Hello world.
The first element after those removed is: d.
The modified string object str1 is: Held.
The original string object str2 is: Hello World.
The first element after those removed is: W.
The modified string object str2 is: HelloWorld.
The original string object str3 is: Hello computer.
The modified string object str3m is: Hello .