C++ List 库 - clear() 函数
描述
C++ 函数 std::list::clear() 通过从列表中删除所有元素并将列表的大小设置为零来销毁列表。
声明
以下是 std::list::clear() 函数形式 std::list 头的声明。
C++98
void clear();
C++11
void clear() noexcept;
参数
None
返回值
None
异常
此成员函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::list::clear() 函数的用法。
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 2, 3, 4, 5};
cout << "Initial size of list = " << l.size() << endl;
l.clear();
cout << "Size of list after clear operation = " << l.size() << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Initial size of list = 5 Size of list after clear operation = 0

