![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ unordered_multimap::emplace() Function
The C++ std::unordered_multimap::emplace() function is used to insert the new element and extends the container size by one.
If the same key is emplaced multiple times, then the unordered_multimap stores every key or value because the unordered_multimap is a container that stores duplicate or multiple keys of the same value.
Syntax
Following is the syntax of std::unordered_multimap::emplace() function.
iterator emplace(Args&&... args );
Parameters
- args − It is the arguments to forward to the constructor of the element.
Return value
This function returns an iterator to newly inserted element.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_multimap::emplace() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; umm.emplace('b', 2); umm.emplace('c', 3); cout << "unordered multimap contains following elements" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
If we run the above code it will generate the following output −
unordered multimap contains following elements a = 1 b = 2 b = 2 c = 3 c = 3 d = 4 e = 5
Example 2
In the following example, we are going to the add more keys and values to the unordered_multimap.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<string, int> umm = {{"Aman", 490},{"Vivek", 485},{"Akash", 500},{"Sonam", 450}}; cout << "Unordered multimap contains following elements before" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; cout<<"after use of the emplace function \n"; umm.emplace("Sarika", 440); umm.emplace("Sonam", 480); cout << "Unordered multimap contains following elements" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Following is the output of the above code −
unordered multimap contains following elements before Akash = 500 Vivek = 485 Sonam = 450 Aman = 490 after use of the emplace function Unordered multimap contains following elements Aman = 490 Sonam = 480 Sonam = 450 Vivek = 485 Akash = 500 Sarika = 440
Example 3
Consider the following example, wehere we are trying to change the value of an already present key using the emplace() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<int, int> umm = {{10, 100},{20, 200},{30, 300},{40, 400}}; cout << "Unordered map contains following elements before usages of emplace" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; umm.emplace(20, 440); umm.emplace(30, 460); cout << "Unordered map contains same elements after the usages of emplace() function" << endl; for (auto it = umm.begin(); it != umm.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Output of the above code is as follows −
Unordered map contains following elements before usages of emplace 40 = 400 30 = 300 20 = 200 10 = 100 Unordered map contains same elements after the usages of emplace() function 10 = 100 20 = 440 20 = 200 30 = 460 30 = 300 40 = 400