C++ 字串庫 - clear



描述

它清除字串的內容,使字串變成空字串。

宣告

以下是 std::string::clear 的宣告。

void clear();

C++11

void clear() noexcept;

引數

返回值

異常

如果丟擲異常,則字串不會發生任何更改。

示例

以下為 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 *
string.htm
廣告