公共成員函式
<string>

std::string::empty

bool empty() const;
bool empty() const noexcept;
測試字串是否為空
返回字串是否為空 (即,它的長度是否為0).

此函式不會以任何方式修改字串的值。 要清除字串的內容,請參見string::clear

引數



返回值

true如果 字串長度0, false否則為 false。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// string::empty
#include <iostream>
#include <string>

int main ()
{
  std::string content;
  std::string line;
  std::cout << "Please introduce a text. Enter an empty line to finish:\n";
  do {
    getline(std::cin,line);
    content += line + '\n';
  } while (!line.empty());
  std::cout << "The text you introduced was:\n" << content;
  return 0;
}

這個程式逐行讀取使用者輸入,並將其儲存到字串中內容直到輸入一個空行為止。

複雜度

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

迭代器有效性

沒有變化。

資料競爭

該物件被訪問。

異常安全

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

另見