C++98
void clear();
Clear string
Erases the contents of the string, which becomes an empty string (with a length of 0 characters).
清除字串的內容值, 使之成為空字串.
感覺上干涉到 cout 的一些執行方式, 需要再去查看.
// string::clear
#include <iostream>
#include <string>
int main ()
{
char c;
std::string str;
std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
do {
c = std::cin.get();
str += c;
if (c=='\n')
{
std::cout << str;
str.clear();
}
} while (c!='.');
return 0;
}
This program repeats every line introduced by the user until a line contains a dot ('.'). Every newline character ('\n') triggers the repetition of the line and the clearing of the current string content.
程式輸入使用者內容值, 只要碰到 '\n' 則輸出到螢幕上顯示,
再用 clear() 清空字串.
Please type some lines of text. Enter a dot (.) to finish:
a < input
a < output
b < input
b < output
cc dd
cc dd
.
離開時使用 '.' 作為離開字元.
如果將 str.clear(); 註解.
變回得到以下輸出:
Please type some lines of text. Enter a dot (.) to finish:
a < input
a < output
b < input
a < output
b < output
cc dd < input
a < output
b < output
cc dd < output
.
可以看到前次的輸入將會在下一的輸出中顯示.
參考網址: http://www.cplusplus.com/reference/string/string/clear/
沒有留言:
張貼留言