unordered_multimap::emplace_hint()是C++ STL中的內置函數,該函數在unordered_multimap容器中插入新的{key:element}。它從參數中提供的位置開始搜索元素的插入點。該位置僅用作提示,它不決定要進行插入的位置。插入會根據容器的標準自動在該位置進行。它將容器的尺寸增加了一個。
用法:
unordered_multimap_name.emplace_hint(iterator position, key, element)
參數:該函數接受三個強製性參數,如下所述:
- position:它指定迭代器,該迭代器指向從中開始搜索插入操作的位置。
- key:它指定要插入到容器中的 key 。
- element:它指定要在容器中插入的元素
返回值:它返回一個指向新插入元素的迭代器。
以下示例程序旨在說明上述函數:
示例1:
// C++ program to illustrate
// unordered_multimap::emplace_hint()
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> 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 <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<string, string> 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++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ real()用法及代碼示例
- C++ valarray end()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ valarray cos()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multimap emplace_hint() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。