std::multimap::merge(3)
π¦ std::multimap::merge(3) β C++ Standard Libary manual π¦
okay bestie, welcome to the std::multimap::merge(3) page from the C++ Standard Libary manual β yes, βLibary,β we keep the typo, itβs part of the lore π
β¨
πΈ NAME πΈ
std::multimap::merge β std::multimap::merge (thatβs the name, no cap, itβs giving self-titled era π)
β¨ Synopsis β¨
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 );
It attempts to extract (βspliceβ) each element in source and insert it into *this using the comparison object of *this. π«
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. Obsessed with a no-copy glow-up, honestly π«Ά
β οΈ heads up bestie: the behavior is undefined if get_allocator() != source.get_allocator(). That is not a vibe check you want to fail, fr fr π
π Parameters π
sourceβ compatible container to transfer the nodes from
π Return value π
(none) β yep, nothing comes back, it just does the thing. iconic behavior π
π₯ Exceptions π₯
Does not throw unless comparison throws. Thatβs the only exception path, ngl π₯
π§ Complexity π§
N * log(size() + N)), where N is source.size(). Keep that exactly as written, bestie π
π Example π
// Run this code
#include <iostream>
#include <map>
#include <string>
int main()
{
std::multimap<int, std::string> ma{{1, "apple"}, {5, "pear"}, {10, "banana"}};
std::multimap<int, std::string> mb{{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}};
std::multimap<int, std::string> u;
u.merge(ma);
std::cout << "ma.size(): " << ma.size() << '\n';
u.merge(mb);
std::cout << "mb.size(): " << mb.size() << '\n';
for (auto const& kv : u)
std::cout << kv.first << ", " << kv.second << '\n';
}
Output:
ma.size(): 0
mb.size(): 0
1, apple
2, zorro
4, batman
5, pear
5, X
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)
kisses from the C++ Standard Libary girls π source http://cppreference.com, dated 2024.06.10, and this is std::multimap::merge(3) signing off β 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