A RetroSearch Logo

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

Search Query:

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

unordered_multiset emplace_hint() function in C++ STL

unordered_multiset emplace_hint() function in C++ STL

Last Updated : 23 Jun, 2022

The unordered_multiset::emplace_hint() is a built-in function in C++ STL which inserts a new element in the unordered_multiset 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_multiset_name.emplace_hint(iterator position, val)

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

Return Value: It returns an iterator which points to the newly inserted element. Below programs illustrates the above function:

 Program 1: 

CPP
// C++ program to illustrate the 
// unordered_multiset::emplace_hint() 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 

    // declaration 
    unordered_multiset<int> sample; 

    // inserts element using emplace_hint() 

    // fast insertions as the search starts 
    // from the previously inserted positions 
    auto it = sample.emplace_hint(sample.begin(), 11); 
    it = sample.emplace_hint(it, 11); 
    it = sample.emplace_hint(it, 11); 

    // slow insertions as the search starts from the 
    // beginning of the containers 
    sample.emplace_hint(sample.begin(), 12); 
    sample.emplace_hint(sample.begin(), 13); 
    sample.emplace_hint(sample.begin(), 13); 
    sample.emplace_hint(sample.begin(), 14); 

    cout << "Elements: "; 

    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << *it << " "; 
    return 0; 
} 
Output:
Elements: 14 11 11 11 12 13 13

Time Complexity: O(n)

Auxiliary Space: O(n)

Program 2: 

CPP
// C++ program to illustrate the 
// unordered_multiset::emplace_hint() function 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 

    // declaration 
    unordered_multiset<char> sample; 

    // inserts element using emplace_hint() 

    // fast insertions as the search starts 
    // from the previously inserted positions 
    auto it = sample.emplace_hint(sample.begin(), 'a'); 
    it = sample.emplace_hint(it, 'a'); 
    it = sample.emplace_hint(it, 'a'); 
    it = sample.emplace_hint(it, 'b'); 

    // slow insertions as the search starts from the 
    // beginning of the containers 
    sample.emplace('b'); 
    sample.emplace('c'); 
    sample.emplace('d'); 

    cout << "Elements: "; 

    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << *it << " "; 
    return 0; 
} 
Output:
Elements: d a a a b b c

Time Complexity: O(n)

Auxiliary Space: O(n)



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