std::multimap::merge(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ¦‹ 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