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



描述

C++ 构造函数std::map::map()构造一个空映射,其中包含零个元素。

声明

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

C++98

explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());

C++11

explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);

参数

  • comp − 一个二元谓词,它接受两个键参数,如果第一个参数在第二个参数之前则返回 true,否则返回 false。默认情况下,它使用less<key_type>谓词。

  • alloc − 分配器对象。

返回值

构造函数不返回值。

异常

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

时间复杂度

常数,即 O(1)

示例

以下示例演示了 `std::map::map()` 构造函数的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m;

   cout << "Size of map = " << m.size() << endl;

   return 0;
}

让我们编译并运行上述程序,这将产生以下结果:

Size of map = 0
map.htm
广告