std::unordered_map::merge(3)
β¨ std::unordered_map::merge β¨
okay bestie, welcome to std::unordered_map::merge from the C++ Standard Libary β yes, exactly as printed β and the page name is std::unordered_map::merge(3). We are gonna splice some nodes and keep every single fact intact. no cap. π
π NAME π
std::unordered_map::merge β std::unordered_map::merge. That's the name, bestie; yes it's the same words twice, and honestly? iconic. π
π SYNOPSIS π
template< class H2, class P2 >
void merge( std::unordered_map<Key, T, H2, P2, Allocator>& source
(1) (since C++17)
);
template< class H2, class P2 >
void merge( std::unordered_map<Key, T, H2, P2, Allocator>&& source
(2) (since C++17)
);
template< class H2, class P2 >
void merge( std::unordered_multimap<Key, T, H2, P2, Allocator>&
(3) (since C++17)
source );
template< class H2, class P2 >
void merge( std::unordered_multimap<Key, T, H2, P2, Allocator>&&
(4) (since C++17)
source );
π«Ά DESCRIPTION π«Ά
Okay so what this actually 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. periodt. π«Ά
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. π
Warning bestie: the behavior is undefined if get_allocator() != source.get_allocator(). Do not play with that. π
πΈ PARAMETERS πΈ
sourceβ compatible container to transfer the nodes from. π«Ά
π RETURN VALUE π
(none) β literally nothing comes back, bestie. π
β COMPLEXITY β
Average case O(N), worst case O(N * size() + N), where N is source.size(). Keep that in your notes. β¨
π§ EXAMPLE π§
// Run this code
#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>
// print out a std::pair
template<class Os, class U, class V>
Os& operator<<(Os& os, const std::pair<U,V>& p)
{
return os << '{' << p.first << ", " << p.second << '}';
}
// print out an associative container
template<class Os, class K, class V>
Os& operator<<(Os& os, const std::unordered_map<K, V>& v)
{
os << '[' << v.size() << "] {";
bool o{};
for (const auto& e : v)
os << (o ? ", " : (o = 1, "")) << e;
return os << "}\n";
}
int main()
{
std::unordered_map<std::string, int>
p{{"C", 3}, {"B", 2}, {"A", 1}, {"A", 0}},
q{{"E", 6}, {"E", 7}, {"D", 5}, {"A", 4}};
std::cout << "p: " << p << "q: " << q;
p.merge(q);
std::cout << "p.merge(q);\n" << "p: " << p << "q: " << q;
}
π POSSIBLE OUTPUT π
p: [3] {{A, 1}, {B, 2}, {C, 3}}
q: [3] {{A, 4}, {D, 5}, {E, 6}}
p.merge(q);
p: [5] {{E, 6}, {D, 5}, {A, 1}, {B, 2}, {C, 3}}
q: [1] {{A, 4}}
π SEE ALSO π
extractβ extracts nodes from the container.(C++17)(public member function)insertβ inserts elements or nodes.(since C++17)(public member function)
okay that's the whole page, bestie β footer deets: source/version http://cppreference.com, date 2024.06.10. bye ππ
π¦ 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