Open In App

unordered_multimap emplace() function in C++ STL

Last Updated : 08 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The unordered_multimap::emplace() is a built-in function in C++ STL which inserts a new {key, element} in the unordered_multimap container. 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(key, element)

Parameters: The function accepts a single mandatory parameter key and element which is to be inserted in the container.

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

Below programs illustrates the above function:

Program 1:




// C++ program to illustrate
// unordered_multimap::emplace()
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and elements
    sample.emplace(1, 2);
    sample.emplace(1, 2);
    sample.emplace(1, 3);
    sample.emplace(4, 9);
    sample.emplace(60, 89);
  
    cout << "Key and Elements: \n";
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << "{" << it->first << ":" << it->second << "}\n ";
  
    return 0;
}


Output:

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

Program 2:




// unordered_multimap::emplace
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<string, string> sample;
  
    // inserts key and elements
    sample.emplace("gopal", "dave");
    sample.emplace("gopal", "dave");
    sample.emplace("Geeks", "C++");
    sample.emplace("multimap", "functions");
    sample.emplace("multimap", "functions");
  
    cout << "Key and Elements: \n";
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << "{" << it->first << ":" << it->second << "}\n ";
  
    return 0;
}


Output:

Key and Elements: 
 {multimap:functions}
 {multimap:functions}
 {Geeks:C++}
 {gopal:dave}
 {gopal:dave}


Previous Article
Next Article

Similar Reads

vector emplace() function in C++ STL
The vector::emplace() is an STL in C++ which extends the container by inserting a new element at the position. Reallocation happens only if there is a need for more space. Here the container size increases by one.Syntax: template iterator vector_name.emplace (const_iterator position, element); Parameter: The function accepts two mandatory parameter
3 min read
list emplace() function in C++ STL
The list::emplace() is a built-in function in C++ STL which extends list by inserting new element at a given position. Syntax: list_name.emplace(position, element)Parameters: The function accepts two mandatory parameters which are described below: position - it specifies the iterator which points to the position in the list where the new element is
2 min read
unordered_set emplace() function in C++ STL
The unordered_set::emplace() function is a built-in function in C++ STL which is used to insert an element in an unordered_set container. The element is inserted only if it is not already present in the container. This insertion also effectively increases the container size 1.Syntax: unordered_set_name.emplace(element) Parameter: This function acce
2 min read
unordered_multiset emplace() function in C++ STL
The unordered_multiset::emplace() is a built-in function in C++ STL which inserts a new element in the unordered_multiset container. 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(val) Parameters: The function accepts
2 min read
unordered_multimap begin() and end() function in C++ STL
The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
5 min read
unordered_multimap key_eq() function in C++ STL
unordered_multimap::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_multimap container. The key equivalence comparison is a predicate which takes two arguments and returns a boolean value indicating whether they are to
2 min read
unordered_multimap max_bucket_count() function in C++ STL
The unordered_multimap::max_bucket_count() is a built-in function in C++ STL which returns the maximum number of buckets that the unordered multimap container can have. This is the maximum it can have, it cannot exceed despite the collisions due to certain limitations on it. Syntax: unordered_multimap_name.max_bucket_count() Parameter: The function
2 min read
unordered_multimap find() function in C++ STL
The unordered_multimap::find() is a built-in function in C++ STL which returns an iterator which points to one of the elements which has the key k. If the container does not contain any element with key k, then it returns an iterator which points to the position which is past the last element in the container. Syntax: unordered_multimap_name.find(k
3 min read
unordered_multimap load_factor() function in C++ STL
The unordered_multimap::load_factor() is a built-in function in C++ STL which returns returns the current load factor in the unordered_multimap container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count The load factor influences the
3 min read
unordered_multimap equal_range() function in C++ STL
unordered_multimap::equal_range() is a built-in function in C++ STL which returns the range in which all the element's key is equal to a key. It returns a pair of iterators where the first is an iterator pointing to the lower bound of the range and second is an iterator pointing to the upper bound of the range. If there is no element equal to a giv
3 min read
Practice Tags :