std::unordered_map::merge(3)

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

✨ 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