std::unorde...iset::merge(3)
π std::unordered_multiset::merge β bestie edition π
okay so this is the std::unorde...iset::merge(3) page from the C++ Standard Libary manual β yes, that's how βLibaryβ is spelled up top, and no, we're not fixing it, it's iconic. π
π NAME π
std::unordered_multiset::merge β literally std::unordered_multiset::merge. that's the whole name, bestie. β¨
β¨ Synopsis β¨
bestie, there are four overloads and they're all since C++17. here they are, exactly:
(1) (since C++17)
template< class H2, class P2 >
void merge( std::unordered_set<Key, H2, P2, Allocator>& source );
(2) (since C++17)
template< class H2, class P2 >
void merge( std::unordered_set<Key, H2, P2, Allocator>&& source );
(3) (since C++17)
template< class H2, class P2 >
void merge( std::unordered_multiset<Key, H2, P2, Allocator>& source );
(4) (since C++17)
template< class H2, class P2 >
void merge( std::unordered_multiset<Key, H2, P2, Allocator>&& source );
so what does it do? it attempts to extract ("splice") each element in source and insert it into *this using the hash function and key equality predicate of *this. π«Ά
and here's the cute part: no elements are copied or moved β only the internal pointers of the container nodes are repointed. so all pointers and references to the transferred elements remain valid, but now refer into *this, not into source. π«
but bestie, listen: iterators referring to the transferred elements and all iterators referring to *this are invalidated. π
β οΈ DANGER, bestie: the behavior is undefined if get_allocator() != source.get_allocator(). like, undefined behavior is not the vibe. do not do that. π
π«Ά Parameters π«Ά
sourceβ compatible container to transfer the nodes from. that's it, that's the parameter. π
π« Return value π«
(none)β nothing comes back. it does the thing and dips. π¦
π₯ Complexity π₯
average case O(N), worst case O(N * size() + N), where N is source.size(). slay. β¨
π Example π
the page says // Run this code, so run it, bestie:
#include <iostream>
#include <unordered_set>
// print out a container
template<class Os, class K>
Os& operator<<(Os& os, const std::unordered_multiset<K>& v)
{
os << '[' << v.size() << "] {";
bool o{};
for (const auto& e : v)
os << (o ? ", " : (o = 1, " ")) << e;
return os << " }\n";
}
int main()
{
std::unordered_multiset<char>
p{'C', 'B', 'B', 'A'},
q{'E', 'D', 'E', 'C'};
std::cout << "p: " << p << "q: " << q;
p.merge(q);
std::cout << "p.merge(q);\n" << "p: " << p << "q: " << q;
}
π Possible output π
p: [4] { A, B, B, C }
q: [4] { C, D, E, E }
p.merge(q);
p: [8] { E, E, D, A, B, B, C, C }
q: [0] { }
look at q after the merge: [0] { }. empty. gone. she transferred everything and said periodt. π
π See also π
extractβ extracts nodes from the container.(C++17)(public member function)πΈinsertβ inserts elements or nodes.(since C++17)(public member function)β
π«Ά that's the whole page, bestie. footer vibe check: sourcehttp://cppreference.com, version/date2024.06.10, pagestd::unorde...iset::merge(3). no cap, that's the sign-off. π
π¦ 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