std::map::merge(3)

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

πŸ’… 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