How Can I Get All the Unique Keys in a Multimap? Last Updated : 29 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, multimap stores key-value pairs like map but they allow users to store duplicate keys. It means that the same key can occur multiple times. In this article, we will learn how to extract the unique key from a multimap Example Input: myMmap = { {1, 'a' }, {2, 'p'}, {3, 'e'}, {2, 'r'}, {4, 'c'}, {3, 'x'} } Output: {1, 2, 3, 4}Extract Unique Keys from a Multimap in C++We can create a set container and insert all the keys of the multimap in that set. The set container only stores the unique values so the duplicate keys will already be rejected. C++ Program to Extract the Keys from a Multimap C++ // C++ program to demonstrate the use of a set to extract // unique keys in a multimap. #include <iostream> #include <map> #include <set> using namespace std; int main() { // Create and populate a multimap with some values multimap<int, char> mymm = { { 1, 'a' }, { 2, 'p' }, { 3, 'e' }, { 2, 'r' }, { 4, 'c' }, { 3, 'x' } }; // Create a set to store unique keys set<int> uniqueKeys; for (auto& it : mymm) { // Collect unique keys in the set uniqueKeys.insert(it.first); } // Print unique keys cout << "Unique Keys:" << endl; for (const auto& key : uniqueKeys) { cout << key << " "; } return 0; } OutputUnique Keys: 1 2 3 4 Explanation: In the above example we use a std::set named uniqueKeys to store the unique keys from the multimap. The for loop iterates through the multimap, and the upper_bound function is used to jump to the next different key in the multimap. Inside the loop, we insert each unique key into the set. We can also use unordered_set here in order to do the insertion faster but in this process we lose the order of the keys. Comment More infoAdvertise with us Next Article How To Delete Multiple Key-Value Pairs From Multimap in C++? L lunatic1 Follow Improve Article Tags : C++ Programs C++ cpp-set cpp-multimap CPP Examples +1 More Practice Tags : CPP Similar Reads How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read How To Find All Occurrences of a Key in a Multimap in C++? In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++. Example: Input:myMutimap = {{ "id", "111" }, { "id", "112" }, { 2 min read How to Find Frequency of a Key in a Multimap in C++? In C++, Multimap is similar to a map that stores the data in the key-value format but the difference between these two containers is that we can have multiple elements with the same keys. In this article, we will learn how to find the frequency of a specific key in a multimap in C++. Example Input: 2 min read How to Access a Value in a Map Using a Key in C++? In C++, std::map container store data in the form of key-value pairs where keys are associated with the given value. In this article, we will learn how to access a value in a std::map using key in C++.ExamplesInput: m = {{'a', 11}, {'b', 45}, {'c', 9}}, k = 'c'Output: 9Explanation: The key 'c' prese 3 min read How To Delete Multiple Key-Value Pairs From Multimap in C++? In C++, a multimap is a container that stores elements where each element has a key value and a mapped value. Unlike maps, multimaps allow multiple key-value pairs with the same key. In this article, we will learn how to delete multiple key-value pairs from a multimap in C++. Example Input: myMultim 2 min read How To Insert Multiple Key-Value Pairs Into a Multimap in C++? In C++, a multimap is similar to a map that stores the data in the key-value format and it also allows us to store duplicate keys for the same value. In this article, we will learn how to insert multiple Key-Value pairs efficiently into a multimap. Example: Input: multi_map = {{"Manas","Singing" }, 2 min read Like