How to Find the Union of Two Maps in C++? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, maps are associative containers that store key-value pairs. The union of two maps means combining their elements while ensuring that duplicate keys are handled appropriately. In this article, we will learn how to find the union of two maps in C++. For Example, Input: mapA = {{'a', 1}, {'b', 2}}; mapB = {{'b', 3}, {'c', 4}}; Output: Map After Union: a: 1 b: 3 c: 4 Merge Two Maps in C++To find the union of two std::maps, we can use the std::map::insert function that accepts iterators pointing to the beginning and end of another map whose elements need to be added to the original map. Maps already stores the unique elements so there will be no issues regarding duplicates. Syntax to Find the Union of Two Mapsmap1.insert(map2.begin() , map2.end()); C++ Program to Merge Two MapsThe below program demonstrates how we can use std::map::insert() function to find union of two maps in C++. C++ // C++ program to illustrate how to find union of two maps #include <iostream> #include <map> using namespace std; int main() { // creating two maps to be merged map<char, int> mapA = { { 'a', 1 }, { 'b', 2 } }; map<char, int> mapB = { { 'b', 3 }, { 'c', 4 } }; // Merge elements from mapB into mapA mapA.insert(mapB.begin(), mapB.end()); // Print the merged map for (const auto& pair : mapA) { cout << pair.first << ": " << pair.second << endl; } return 0; } Outputa: 1 b: 2 c: 4 Time Complexity: O(n + m), here n and m are the number of elements in the first and second map respectively.Auxilliary Space: O(n + m) Note: We can also std::map::merge function to find union of two maps while using C++ 17 and newer. Comment More infoAdvertise with us Next Article How to Find the Union of Two Maps in C++? B bug8wdqo Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Union of Two Multimaps in C++? In C++, finding the union of two multimaps consists of combining the elements from both multimap collections while considering the duplicates as a multimap allows multiple values to have the same key. In this article, we will learn to find the union of two multimaps in C++. Example:Input: multi1 = { 2 min read How to Find the Union of Two Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how to find the union of two sets in C++. Example Input:set1 = {12 , 13, 14 2 min read How to Find the Union of Two Deques in C++? In C++, deques also called double-ended queues are sequence containers that can expand and contract on both ends. The union of two deques means the set of elements that consists of all the elements of the two deques at once. In this article, we will learn how to find the union of two deques in C++. 3 min read How to Find Union of Two Multisets in C++? In C++, multisets are a type of associative container similar to the set, with the exception that multiple elements can have the same values. Union is In this article, we will see how to find the union of two multisets in C++ STL. Example Input: first_multiset = {100,300,400,500,200,500,200}, second 3 min read How to Find the Difference of Two Maps in C++? In C++, a map is a container that stores key-value pairs in an ordered or sorted manner. Finding the difference between two maps involves determining the elements that are present in one map but not in the other. In this article, we will learn how to find the difference between two Maps in C++. Exam 2 min read Like