
- 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>
- 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_set::emplace() Function
TheC++ std::unordered_set::emplace() function is used to insert a new element in the unordered_set if its value is unique. The insertion will only takes place if the element does not exists in the unordered_set. when this function is invoked, it insert a new element avoiding the unneccasry copy or move operations.
Syntax
Following is the syntax of std::unordered_set::emplace() function.
pair <iterator,bool> emplace ( Args&&... args );
Parameters
- args − It indicates the arguments passed to the constructor.
Return Value
This function returns an element and a boolean value pair. If the function successfully inserts the element, it returns a pair with an iterator for the newly inserted element and a value of true. If the element to be inserted is already present in the unordered_set, then it returns a pair of iterators pointing to the already-present element and a boolean value of false.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::emplace() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset; myset.emplace ("kittu"); myset.emplace ("prasad"); myset.emplace ("sairamkrishna"); std::cout << "myset containing:"; for (const std::string& x: myset) std::cout << " " << x; std::cout << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
myset containing: sairamkrishna prasad kittu
Example 2
Consider the following example, where we are going to use the emplace() function and inserting the element that is not present in the unordered_set.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> myUset={10, 20}; myUset.emplace (30); myUset.emplace (40); myUset.emplace (50); cout << "myUset containing:"; for (const int& x: myUset) cout << " " << x; cout << endl; return 0; }
Output
If we run the above code it will generate the following output −
myUset containing: 50 40 30 20 10
Example 3
In the following example, we are going to insert the element which was already present in the unordered_set using the emplace() function and observing the output.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> myUset={10, 20, 30, 40}; myUset.emplace (30); myUset.emplace (40); cout << "myUset containing:"; for (const int& x: myUset) cout << " " << x; cout << endl; return 0; }
Output
Following is the output of the above code −
myUset containing: 40 30 20 10
Example 4
Following is the example, where we are going to use the emplace() function and inserting the element into the unordered_set, if the element exists in the unordered_set then it will doesn't make any changes.
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_set<int> myUset{10, 20, 30}; auto Insert = myUset.emplace(40); if(!Insert.second) cout<<"40 is already present in myUset.\n"; else cout<<"40 is added in myUset.\n"; Insert = myUset.emplace(20); if(!Insert.second) cout<<"20 is already present in myUset.\n"; else cout<<"20 is added in myUset.\n"; return 0; }
Output
Output of the above code is as follows −
40 is added in myUset. 20 is already present in myUset.