public member function
<list>

std::list::empty

bool empty() const;
bool empty() const noexcept;
测试容器是否为空
Returns whether the list container is empty (i.e. whether its size is0).

This function does not modify the container in any way. To clear the content of a list container, see list::clear.

参数



返回值

trueif the container size is0, false否则为 false。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// list::empty
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  int sum (0);

  for (int i=1;i<=10;++i) mylist.push_back(i);

  while (!mylist.empty())
  {
     sum += mylist.front();
     mylist.pop_front();
  }

  std::cout << "total: " << sum << '\n';
  
  return 0;
}
The example initializes the content of the container to a sequence of numbers (form 1 to 10). It then pops the elements one by one until it is empty and calculates their sum.

输出
total: 55


复杂度

常量。

迭代器有效性

没有变化。

数据竞争

访问容器。
不访问任何包含的元素:并发访问或修改它们是安全的。

异常安全

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

另见