STL unordered_map emplace

STL unordered_map emplace教程

如果我们需要向 STL 中的 unordered_map 容器插入元素除了使用下标索引的形式和使用 insert 方法插入元素,还可以使用 emplace 方法和 emplace_hint 方法。

实现相同的插入操作,无论是用 emplace() 还是 emplace_hint(),都比 insert() 方法的效率高,和 insert() 方法相比,emplace() 和 emplace_hint() 方法的使用要简单很多,因为它们各自只有一种语法格式。

STL unordered_map emplace详解

语法

template <class... Args> pair<iterator,bool> emplace (Args&&... args);

参数

参数 说明
args 需要插入的键。

说明

这里只需要将创建新键值对所需的数据作为参数直接传入即可,此方法可以自行利用这些数据构建出指定的键值对。另外,该方法的返回值也是一个 pair 对象,其中 pair.first 为一个迭代器,pair.second 为一个 bool 类型变量:
当该方法将键值对成功插入到 unordered_map 容器中时,其返回的迭代器指向该新插入的键值对,同时 bool 变量的值为 true;

当插入失败时,则表明 unordered_map 容器中存在具有相同键的键值对,此时返回的迭代器指向此具有相同键的键值对,同时 bool 变量的值为 false。

STL unordered_map emplace_hint详解

语法

template <class... Args> iterator emplace_hint (const_iterator position, Args&&... args);

参数

参数 说明
position 需要插入的位置。
args 需要插入的键。

说明

和 emplace() 方法相同,emplace_hint() 方法内部会自行构造新键值对,因此我们只需向其传递构建该键值对所需的 2 个元素(第一个作为键,另一个作为值)即可。不同之处在于:

  • emplace_hint() 方法的返回值仅是一个迭代器,而不再是 pair 类型变量。当该方法将新键值对成功添加到容器中时,返回的迭代器指向新添加的键值对;反之,如果添加失败,该迭代器指向的是容器中和要添加键值对键相同的那个键值对。
  • emplace_hint() 方法还需要传递一个迭代器作为第一个参数,该迭代器表明将新键值对添加到容器中的位置。需要注意的是,新键值对添加到容器中的位置,并不是此迭代器说了算,最终仍取决于该键值对的键的值。

可以这样理解,emplace_hint() 方法中传入的迭代器,仅是给 unordered_map 容器提供一个建议,并不一定会被容器采纳。

案例

unordered_map emplace插入元素

使用 emplace 给 unordered_map 插入元素

#include <iostream> #include <unordered_map> using namespace std; int main() { cout << "嗨客网(www.haicoder.net)\n" << endl; //创建并初始化 unordered_map 容器 std::unordered_map<string, string>myunordered_map; //插入键值对 pair<unordered_map<string, string>::iterator, bool> ret = myunordered_map.emplace("CPP教程", "https://haicoder.net/cpp/cpp-tutorial.html"); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //插入新键值对 ret = myunordered_map.emplace("C语言教程", "https://haicoder.net/c/c-tutorial.html"); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //失败插入的样例 ret = myunordered_map.emplace("CPP教程", "https://haicoder.net/golang/golang-tutorial.html"); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; return 0; }

因为,这里需要使用 C++ 11,因此,我们在 Linux 下使用 g++ 进行编译,具体命令如下:

g++ unordered_map.cpp -std=c++11

编译后,我们直接运行生成的二进制文件 a.out,如下图所示:

12_unordered_map emplace.png

我们看到,我们使用了 emplace 将元素插入到了 unordered_map 中。

unordered_map emplace_hint插入元素

使用 emplace_hint 给 unordered_map 插入元素

#include <iostream> #include <unordered_map> using namespace std; int main() { cout << "嗨客网(www.haicoder.net)\n" << endl; //创建并初始化 unordered_map 容器 std::unordered_map<string, string>myunordered_map; //指定在 unordered_map 容器插入键值对 unordered_map<string, string>::iterator iter = myunordered_map.emplace_hint(myunordered_map.begin(),"CPP教程", "https://haicoder.net/cpp/cpp-tutorial.html"); cout << iter->first << " " << iter->second << endl; iter = myunordered_map.emplace_hint(myunordered_map.begin(), "C语言教程", "https://haicoder.net/c/c-tutorial.html"); cout << iter->first << " " << iter->second << endl; //插入失败样例 iter = myunordered_map.emplace_hint(myunordered_map.begin(), "CPP教程", "https://haicoder.net/cpp/cpp-tutorial.html"); cout << iter->first << " " << iter->second << endl; return 0; }

编译后,我们直接运行生成的二进制文件 a.out,如下图所示:

13_unordered_map emplace_hint.png

我们看到,我们使用了 emplace_hint 将元素插入到了 unordered_map 中。

STL unordered_map emplace总结

无论是用 emplace() 还是 emplace_hint(),都比 insert() 方法的效率高,和 insert() 方法相比,emplace() 和 emplace_hint() 方法的使用要简单很多,因为它们各自只有一种语法格式。