Erase

Erase

Erase:vt.擦掉; 抹去; 擦掉; 清除;

Erase 語句 (Visual Basic):數組的清除和重新定義,程式是定義了一個靜態數組 T,用 For循環語句為每個數組的元素賦值,然後執行 Erase語句,將各數組元素的值清除。程式的運行結果如下:

– 1 2 3 4 5

– 0 0 0 0 0

基本介紹

簡述,描述,語法,說明,頭檔案,函式原型,參數,返回值,舉例,例一,例二,

簡述

描述

重新初始化固定大小數組的元素,並釋放動態數組的存儲空間。

語法

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.h>

函式原型

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 .

相關詞條

熱門詞條

聯絡我們