字符串内容被设置为空字符串,擦除所有先前的内容,因此其大小保留为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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。