公共成员函数
<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;
}

这个程序逐行读取用户输入,并将其存储到字符串中内容直到输入一个空行为止。

复杂度

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

迭代器有效性

没有变化。

数据竞争

该对象被访问。

异常安全

无异常保证:此成员函数从不抛出异常。

另见