Erase

编辑:妻子网互动百科 时间:2019-12-10 00:13:05
编辑 锁定
Erase:vt.擦掉; 抹去; 擦掉; 清除; [1] 
Erase 语句 (Visual Basic):数组的清除和重新定义,程序是定义了一个静态数组 T,用 For循环语句为每个数组的元素赋值,然后执行 Erase语句,将各数组元素的值清除。程序的运行结果如下:
– 1 2 3 4 5
– 0 0 0 0 0[2] 
中文名
Erase
语    法
Erasearray
释    放
动态数组存储空间
头文件
<string.h>

Erase简述

编辑

Erase描述

重新初始化固定大小数组的元素,并释放动态数组的存储空间。[3] 

Erase语法

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 按照下表设置固定数组的元素:
数组的类型
Erase 对固定数组元素的影响
固定数值数组
将每个元素设置为 0
固定字符串数组
将每个元素设置为零长度字符串
将每个元素设置为特殊值*
*特殊值为Nothing
Erase 释放动态数组所使用的内存。在程序再次引用该动态数组之前,必须使用 ReDim 语句来重新定义该数组变量的维数。

Erase头文件

编辑
<string.h>

Erase函数原型

编辑
iterator erase(
iterator _First,
iterator _Last
);
iterator erase(
iterator _It
);
basic_string& erase(
size_type_Pos = 0,
size_type _Count = npos
);

Erase参数

编辑
_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.

Erase返回值

编辑
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.

Erase举例

编辑

Erase例一

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 ''

Erase例二

// 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 .
参考资料
词条标签:
计算机学