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

