C++ 映射库 - clear() 函数



描述

C++ 函数std::map::clear() 通过移除所有元素并设置映射大小为零来销毁映射。

声明

以下是来自 std::map 头文件的 std::map::clear() 函数的声明。

C++98

void clear();

C+11

void clear() noexcept;

参数

返回值

异常

此成员函数从不抛出异常。

时间复杂度

线性,即 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
map.htm
广告