std::map::merge(3)
π std::map::merge π
okay so this is the C++ Standard Libary page for std::map::merge(3) β yes, βLibaryβ is literally how itβs spelled in the manual, and Iβm not fixing it because that would be disrespectful to the source material π. anyway bestie, this function is lowkey iconic: it moves nodes between maps without copying or moving the actual elements. letβs get into it.
β¨ NAME β¨
std::map::merge - std::map::merge
π Synopsis π
four overloads, all since C++17. hereβs the exact signature lineup, no cap:
template< class C2 > (1)
(since C++17)
void merge( std::map<Key, T, C2, Allocator>& source );
template< class C2 > (2)
(since C++17)
void merge( std::map<Key, T, C2, Allocator>&& source );
template< class C2 > (3)
(since C++17)
void merge( std::multimap<Key, T, C2, Allocator>& source );
template< class C2 > (4)
(since C++17)
void merge( std::multimap<Key, T, C2, Allocator>&& source );
so what does it do? it attempts to extract ("splice") each element in source and insert it into *this using the comparison object of *this. if thereβs already an element in *this with a key equivalent to the key of an element from source, then that element is not extracted from source. 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 they refer into *this, not into source. π«Ά
β οΈ the behavior is undefined if get_allocator() != source.get_allocator(). like, donβt do that, bestie, thatβs not the vibe. π
πΈ Parameters πΈ
sourceβ compatible container to transfer the nodes from
π Return value π
(none) β literally nothing, itβs giving void energy β¨
π¦ Exceptions π¦
Does not throw unless comparison throws.
β Complexity β
N * log(size() + N)), where N is source.size().
π Example π
// Run this code
#include <iostream>
#include <map>
#include <string>
int main()
{
std::map<int, std::string> ma{{1, "apple"}, {5, "pear"}, {10,
"banana"}};
std::map<int, std::string> mb{{2, "zorro"}, {4, "batman"}, {5,
"X"}, {8, "alpaca"}};
std::map<int, std::string> u;
u.merge(ma);
std::cout << "ma.size(): " << ma.size() << '\n';
u.merge(mb);
std::cout << "mb.size(): " << mb.size() << '\n';
std::cout << "mb.at(5): " << mb.at(5) << '\n';
for (auto const& kv : u)
std::cout << kv.first << ", " << kv.second << '\n';
}
π Output π
ma.size(): 0
mb.size(): 1
mb.at(5): X
1, apple
2, zorro
4, batman
5, pear
8, alpaca
10, banana
π See also π
extractβ extracts nodes from the container (C++17) (public member function)insertβ inserts elements or nodes (since C++17) (public member function)
and thatβs the whole page, bestie! xoxo π
source: http://cppreference.com, version/date: 2024.06.10, page: std::map::merge(3). periodt. π
π¦ 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