在本文中,我们将讨论C ++ STL中multimap::emplace_hint()函数的工作,语法和示例。
多图是关联容器,类似于图容器。它还有助于按特定顺序存储由键值和映射值的组合形成的元素。在多图容器中,可以有多个与同一键关联的元素。始终在内部借助关联的键对数据进行排序。
emplace_hint()函数是C ++ STL中的内置函数,在<map>头文件中定义。该函数在位置中的多图容器中插入一个新元素。在emplace_hint()中,我们为元素传递一个位置,该位置充当提示。此功能类似于,emplace()区别在于我们提供了位置提示以插入值。此功能还将多集容器的大小增加1。
multimap_name.emplace_hint(iterator pos, Args& val);
该函数接受以下参数-
pos-这是参数的迭代器类型,用于提供位置提示。
val-这是我们要插入的元素。
此函数将迭代器返回到放置/插入元素的位置。
输入项
std::multimap<char, int> odd, eve;
odd.insert({‘a’, 1});
odd.insert({‘b’, 3});
odd.insert({‘c’, 5});
odd.emplace_hint(odd.end(), {‘d’, 7});输出结果
Odd: a:1 b:3 c:5 d:7
Code:
#include <bits/stdc++.h>
using namespace std;
int main(){
//创建容器
multimap<int, int> mul;
//使用emplace插入
mul.emplace_hint(mul.begin(), 1, 10);
mul.emplace_hint(mul.begin(), 2, 20);
mul.emplace_hint(mul.begin(), 3, 30);
mul.emplace_hint(mul.begin(), 1, 40);
mul.emplace_hint(mul.begin(), 4, 50);
mul.emplace_hint(mul.begin(), 5, 60);
cout << "\nElements in multimap is : \n";
cout << "KEY\tELEMENT\n";
for (auto i = mul.begin(); i!= mul.end(); i++){
cout << i->first << "\t" << i->second << endl;
}
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Elements in multimap is : KEY ELEMENT 1 40 1 10 2 20 3 30 4 50 5 60