C++ String 库 - clear
描述
它擦除字符串的内容,变成一个空字符串。
声明
以下是 std::string::clear 的声明。
void clear();
C++11
void clear() noexcept;
参数
none
返回值
none
异常
如果抛出异常,则字符串没有变化。
示例
在下面的 std::string::clear 示例中。
#include <iostream>
#include <string>
int main () {
char c;
std::string str;
std::cout << "Please type some lines of text. Enter a start (*) to finish:\n";
do {
c = std::cin.get();
str += c;
if (c=='\n') {
std::cout << str;
str.clear();
}
} while (c!='*');
return 0;
}
示例输出应该是这样的 −
Please type some lines of text. Enter a start (*) to finish: sairam.krishna *

