unordered_multimap :: emplace_hint()是C++ STL中的内置函数,该函数在unordered_multimap容器中插入新的{key:element}。它从参数中提供的位置开始搜索元素的插入点。该位置仅用作提示,它不决定要进行插入的位置。插入会根据容器的标准自动在该位置进行。它将容器的尺寸增加了一个。
句法:
unordered_multimap_name.emplace_hint(iterator position, key, element)
参数:该函数接受三个强制性参数,如下所述:
- position:指定迭代器,该迭代器指向从中开始插入搜索操作的位置。
- 密钥:它指定要插入到容器中的密钥。
- element:它指定要插入到容器中的元素
返回值:返回一个迭代器,该迭代器指向新插入的元素。
下面的程序说明了上述函数:
程序1:
// C++ program to illustrate
// unordered_multimap::emplace_hint()
#include
#include
#include
using namespace std;
int main()
{
// declaration
unordered_multimap sample;
// inserts key and element in a faster
// way as hint given is correct
auto it = sample.emplace_hint(sample.begin(), 1, 2);
it = sample.emplace_hint(it, 1, 2);
it = sample.emplace_hint(it, 1, 3);
// slower methods as wrong position
// has beeen given to start
sample.emplace_hint(sample.begin(), 4, 9);
sample.emplace_hint(sample.begin(), 60, 89);
std::cout << "Key and elements:\n";
for (auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "}\n ";
std::cout << std::endl;
return 0;
}
输出:
Key and elements:
{60:89}
{4:9}
{1:2}
{1:2}
{1:3}
程式2:
// C++ program to illustrate
// unordered_multimap::emplace_hint()
#include
#include
#include
using namespace std;
int main()
{
// declaration
unordered_multimap sample;
// inserts elements in a faster way as
// hint given is correct
auto it = sample.emplace_hint(sample.begin(), "gopal", "dave");
it = sample.emplace_hint(it, "gopal", "dave");
it = sample.emplace_hint(it, "Geeks", "Website");
// slower methods as wrong position
// has beeen given to start
sample.emplace_hint(sample.begin(), "Geeks", "STL");
sample.emplace_hint(sample.begin(), "Multimap", "functions");
std::cout << "Key and elements:\n";
for (auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "}\n ";
std::cout << std::endl;
return 0;
}
输出:
Key and elements:
{Multimap:functions}
{Geeks:Website}
{Geeks:STL}
{gopal:dave}
{gopal:dave}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。