请输入您要查询的百科知识:

 

词条 Erase
释义

简述

描述

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

语法

Erase array

array 参数是要清除的数组变量的名称。

说明

判断数组是固定长度数组(常规)还是动态数组是很重要的,这是因为 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.

举例

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

}

输出:

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 .

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/1/11 10:36:32