目录

map::map

描述 (Description)

C ++构造函数std::map::map()从初始化列表构造一个映射。

声明 (Declaration)

以下是std :: map :: map()构造函数形式std :: map头的声明。

C++11

map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());

参数 (Parameters)

  • il - 初始化列表。

  • comp - 二元谓词,它接受两个关键参数,如果第一个参数在第二个参数之前返回,则返回true,否则返回false。 默认情况下,它使用较少的“key_type”谓词。

  • alloc - 分配器对象。

返回值

构造函数永远不会返回值。

异常 (Exceptions)

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

时间复杂

线性即O(n)

例子 (Example)

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

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   /* Initializer_list constructor */
   map<char, int> m1 = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };
   cout << "Map contains following elements" << endl;
   for (auto it = m1.begin(); it != m1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

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

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
↑回到顶部↑
WIKI教程 @2018