public member function
<string>

std::basic_string::clear

void clear();
void clear() noexcept;
清除字串
擦除basic_string的內容,使其變為空字串長度0個字元)。

引數



返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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;
}

此程式重複使用者輸入的每一行,直到該行包含一個點('.')。每個換行符('\n')觸發該行的重複以及當前字串內容的清除。

複雜度

未指定,但通常是恆定的。

迭代器有效性

與此物件相關的任何迭代器、指標和引用都可能失效。

資料競爭

物件被修改。

異常安全

無異常保證:此成員函式從不丟擲異常。

另見