std::unordered_set::merge(3)
โจ std::unordered_set::merge โจ
Hey bestie ๐ this is the page for std::unordered_set::merge(3) from the C++ Standard Libary manual (yes, thatโs the spelling on the page โ iconic ๐
). Itโs giving node-transfer realness, so letโs get into it.
๐ธ NAME ๐ธ
std::unordered_set::merge - std::unordered_set::merge
๐ฆ SYNOPSIS ๐ฆ
Four overloads, all (since C++17), all here to do the thing:
template< class H2, class P2 >
(1) (since C++17)
void merge( std::unordered_set<Key, H2, P2, Allocator>& source );
template< class H2, class P2 >
(2) (since C++17)
void merge( std::unordered_set<Key, H2, P2, Allocator>&& source );
template< class H2, class P2 >
void merge( std::unordered_multiset<Key, H2, P2, Allocator>&
(3) (since C++17)
source );
template< class H2, class P2 >
void merge( std::unordered_multiset<Key, H2, P2, Allocator>&&
(4) (since C++17)
source );
๐ DESCRIPTION ๐
Okay so what it does: it attempts to extract ("splice") each element in source and insert it into *this using the hash function and key equality predicate of *this. If there is an element in *this with key equivalent to the key of an element from source, then that element is not extracted from source โ it stays put. ๐
No elements are copied or moved โ only the internal pointers of the container nodes are repointed. All pointers and references to the transferred elements remain valid, but now refer into *this, not into source. Iterators referring to the transferred elements and all iterators referring to *this are invalidated. Iterators to elements remaining in source remain valid.
โ ๏ธ Undefined behavior alert: the behavior is undefined if get_allocator() != source.get_allocator(). Do not mess with that, bestie. ๐
๐ PARAMETERS ๐
sourceโ compatible container to transfer the nodes from. ๐ง
๐ RETURN VALUE ๐
(none)
๐ฅ COMPLEXITY ๐ฅ
Average case O(N), worst case O(N * size() + N), where N is source.size(). Math girlie moment, no cap. ๐งฎ
๐ EXAMPLE ๐
Hereโs the example exactly as the page gives it โ including the // Run this code marker:
// Run this code
#include <iostream>
#include <unordered_set>
// print out a container
template<class Os, class K>
Os& operator<<(Os& os, const std::unordered_set<K>& v)
{
os << '[' << v.size() << "] {";
bool o{};
for (const auto& e : v)
os << (o ? ", " : (o = 1, " ")) << e;
return os << " }\n";
}
int main()
{
std::unordered_set<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: [3] { A, B, C }
q: [3] { C, D, E }
p.merge(q);
p: [5] { E, D, A, B, C }
q: [1] { C }
๐ SEE ALSO ๐
extractโ extracts nodes from the container.(C++17)(public member function)insertโ inserts elements or nodes.(since C++17)(public member function)
Source: http://cppreference.com ยท version/date: 2024.06.10 ยท page: std::unordered_set::merge(3) โ bye bestie, go merge responsibly ๐โจ
๐ฆ 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