C++ STL中的无序序MultIMAP EngestHin()函数

这个 无序的多重映射::放置提示() 是C++ STL中的一个内置函数,它在无序的MultIMAP容器中插入新的{KE:Eng}。它从参数中提供的位置开始搜索元素的插入点。这个位置只是一个提示,并不决定插入的位置。根据容器的标准,插入会自动在该位置完成。它将容器的大小增加了一倍。

null

语法:

unordered_multimap_name.emplace_hint(iterator position, key, element)

参数: 该函数接受三个强制性参数,如下所述:

  • 职位: 它指定迭代器,该迭代器指向插入搜索操作的起始位置。
  • 关键: 它指定要插入容器中的密钥。
  • 要素: 它指定要插入容器中的元素

返回值: 它返回一个指向新插入元素的迭代器。

以下程序说明了上述功能:

项目1:

C++

// 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 been given to start
sample.emplace_hint(sample.begin(), 4, 9);
sample.emplace_hint(sample.begin(), 60, 89);
std::cout << "Key and elements:" ;
for ( auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "} " ;
std::cout << std::endl;
return 0;
}


输出:

Key and elements:{60:89} {4:9} {1:2} {1:2} {1:3}

项目2:

C++

// 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 been given to start
sample.emplace_hint(sample.begin(), "Geeks" , "STL" );
sample.emplace_hint(sample.begin(), "Multimap" , "functions" );
std::cout << "Key and elements:" ;
for ( auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "} " ;
std::cout << std::endl;
return 0;
}


输出:

Key and elements:{Multimap:functions} {Geeks:Website} {Geeks:STL} {gopal:dave} {gopal:dave}

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享