字符串內容被設置為空字符串,擦除所有先前的內容,因此其大小保留為0個字符。
參數:沒有
返回值:沒有
void string::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing
// CPP code to illustrate
// clear() function
#include <iostream>
#include <string>
using namespace std;
// Function to demo clear()
void clearDemo(string str)
{
// Deletes all characters in string
str.clear();
cout << "After clear:";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before clear:";
cout << str << endl;
clearDemo(str);
return 0;
}
輸出:
Before clear:Hello World! After clear:
相關用法
注:本文由純淨天空篩選整理自 std::string::clear in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。