A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/unordered_multimap-emplace_hint-function-in-c-stl/ below:

unordered_multimap emplace_hint() function in C++ STL

unordered_multimap emplace_hint() function in C++ STL

Last Updated : 17 Dec, 2021

The unordered_multimap::emplace_hint() is a built-in function in C++ STL which inserts a new {key:element} in the unordered_multimap container. It starts searching from the position provided in the parameter for the insertion point of the element. The position only acts as a hint, it does not decide the position at which the insertion is to be done. The insertion is done automatically at the position according to the container's criterion. It increases the size of the container by one. 

Syntax: 

unordered_multimap_name.emplace_hint(iterator position, key, element)

Parameters: The function accepts three mandatory parameters which are described below:  

Return Value: It returns an iterator that points to the newly inserted element. 

Below programs illustrates the above function: 

Program 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:\n"; 
    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << "{" << it->first << ":" << it->second << "}\n "; 
  
    std::cout << std::endl; 
    return 0; 
} 

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

 

Program 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:\n"; 
    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << "{" << it->first << ":" << it->second << "}\n "; 
  
    std::cout << std::endl; 
    return 0; 
} 

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

 

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4