
- 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>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_map::emplace_hint() Function
The C++ functionunordered_map::emplace_hint() inserts a new element in unordered_map using a hint or position. The position only acts as a hint; it does not decide the position at which the insertion is to be done, and it extends the container size by one by inserting the new element. This function is similar to the emplace() function, and insertion takes place only if key is not present,indicating that the key should be unique.
If the same key is emplaced multiple times, the map stores the first element only because the map is a container that does not store multiple keys of the same value.
Syntax
Following is the syntax of unordered_map::emplace_hint() function.
unordered_map.emplace_hint(position, key, element);
Parameters
position − Hint for the position to insert element. This value may be used by the container to optimize the operation.
key − It specifies the key that is to be inserted into the unordered_multimap.
element − It specifies the element/value that is to be inserted into the unordered_multimap.
Return value
Returns an iterator to the newly inserted element. If insertion fails because of already existing element, it returns iterator pointing to the existing element.
Example 1
Consider the following example, where we are demonstrate the usage of the emplace_hint() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void){ unordered_map<char, int> um ={ {'b', 2}, {'c', 3}, {'d', 4}, }; um.emplace_hint(um.end(), 'e', 5); um.emplace_hint(um.begin(), 'a', 1); cout << "Unordered map contains following elements" << endl; for (auto it = um.cbegin(); it != um.cend(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Output of the above code is as follows −
Unordered map contains following elements a = 1 e = 5 d = 4 b = 2 c = 3
Example 2
In the following example, we are going to use the emplace_hint() function and adding keys/values pair into the container at the starting position.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_map<string, int> um = {{"Aman", 490},{"Vivek", 485},{"Akash", 500},{"Sonam", 450}}; cout << "Unordered map contains following elements before" << endl; for (auto it = um.begin(); it != um.end(); ++it) cout << it->first << " = " << it->second << endl; cout<<"after use of the emplace_hint() function \n"; um.emplace_hint(um.begin(), "Sarika", 440); um.emplace_hint(um.begin(), "Satya", 460); cout << "Unordered map contains following elements" << endl; for (auto it = um.begin(); it != um.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
Output
Following is the output of the above code −
Unordered map contains following elements before Sonam = 450 Akash = 500 Vivek = 485 Aman = 490 after use of the emplace_hint() function Unordered map contains following elements Sarika = 440 Satya = 460 Sonam = 450 Akash = 500 Vivek = 485 Aman = 490
Example 3
Let's look at the following example, where we are creating an unordered_map and storing the key-value pairs in random order using the emplace_hint() function.
#include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<int, string> Umap; Umap.emplace_hint(Umap.begin(), 1, "January"); Umap.emplace_hint(Umap.begin(), 2, "February"); Umap.emplace_hint(Umap.begin(), 3, "March"); cout << "The unordered_map is : \n"; cout << "KEY\tELEMENT"<<endl; for (auto itr = Umap.begin(); itr != Umap.end(); itr++) cout << itr->first << "\t"<< itr->second << endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
The unordered_map is : KEY ELEMENT 3 March 2 February 1 January