std::unorde...set::emplace(3)
π std::unordered_set::emplace(3) π
Okay bestie, this page is labeled std::unorde...set::emplace(3) in the C++ Standard Libary manual, and itβs all about std::unordered_set::emplace. The vibe? Constructing elements in place and dodging unnecessary copies or moves if you use it carefully. π
π NAME π
std::unordered_set::emplace β literally std::unordered_set::emplace, bestie. Same name, same function, iconic behavior. π
β¨ Synopsis β¨
Hereβs the signature, no cap:
template< class... Args > (since C++11)
std::pair<iterator, bool> emplace( Args&&... args );
Okay so what it does: inserts a new element into the container, constructed in-place with the args you gave it, but only if thereβs no element with the key already in the container. Periodt.
The constructor of the new element gets called with exactly the same arguments as the ones supplied to emplace, forwarded via std::forward<Args>(args).... And heads up: the element may still be constructed even if there already is an element with the key in the container β in that case the newly constructed element is destroyed immediately. π
Careful use of emplace lets the new element be constructed while avoiding unnecessary copy or move operations. Thatβs the whole flex. β¨
If after the operation the new number of elements is greater than old max_load_factor() * bucket_count(), a rehashing takes place. If rehashing occurs (due to the insertion), all iterators are invalidated. Otherwise (no rehashing), iterators are not invalidated. Important vibe check: iterator invalidation is not a drill. π«Ά
π Parameters π
argsβ the arguments to forward to the constructor of the element.
πΈ Return value πΈ
It returns a std::pair consisting of:
- an iterator to the inserted element (or to the element that prevented the insertion), and
- a
boolvalue set totrueif and only if the insertion took place.
π Exceptions π
If an exception is thrown for any reason, this function has no effect. Thatβs the strong exception safety guarantee, and she ate. π
π₯ Complexity π₯
Amortized constant on average, worst case linear in the size of the container. So: usually slay, worst case lowkey a little yikes. π
π Example π
This section is incomplete Reason: no example
Not me giving you an empty example section, but thatβs literally what the page says. π
π See also π
emplace_hintβ constructs elements in-place using a hint (public member function).insertβ inserts elements or nodes (since C++17) (public member function).
π Category: π
Todo no example
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page label: std::unorde...set::emplace(3) ππ«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms