Take the 2-minute tour ×

I have a multimap and I want get all the unique keys in it to be stored in a vector.

  multimap<char,int> mymm;
  multimap<char,int>::iterator it;
  char c;


How can I do this? there is way to count number of elements with a key but none to count number of unique keys in a multimap.

Added: By unique I mean all the keys in multimap once - they can be repeated or occur once in multimap.

So unique keys here are - x, y and z

share|improve this question

4 Answers 4

up vote 13 down vote accepted

I tried this and it worked

for(  multimap<char,int>::iterator it = mymm.begin(), end = mymm.end(); it != end; it = mymm.upper_bound(it->first))
      cout << it->first << ' ' << it->second << endl;
share|improve this answer
It will print all the elements –  AJ. Jul 19 '12 at 6:27
+1 I didn't think of this approach. Nice. –  Fiktik Jul 19 '12 at 6:32
I want all the keys just once - does not matter if the keys were repeated. –  AJ. Jul 19 '12 at 6:35
@AJ see the upper_bound part, this should print every key only once –  Fiktik Jul 19 '12 at 6:36
@AJ: Are you sure? The code works when I tried it. –  Jesse Good Jul 19 '12 at 6:43

Since the entries of a std::multimap<> are implicitly sorted and come out in sorted order when iterating through them, you can use the std::unique_copy algorithm for this:

#include <iostream>
#include <map>
#include <algorithm>
#include <vector>

using namespace std;

int main() {

  /* ...Your existing code... */

  /* Create vector of deduplicated entries: */
  vector<pair<char,int>> keys_dedup;
              [](const pair<char,int> &entry1,
                 const pair<char,int> &entry2) {
                   return (entry1.first == entry2.first);

  /* Print unique keys, just to confirm. */
  for (const auto &entry : keys_dedup)
    cout << entry.first << '\n';

  return 0;

The extra work added by this is linear in the number of entries of the multimap, whereas using a std::set or Jeeva's approach for deduplication both add O(n log n) computational steps.

Remark: The lambda expression I use assumes C++11. It is possible to rewrite this for C++03.

share|improve this answer

Iterate through all elements of mymm, and store it->first in a set<char>.

share|improve this answer
-1. it will not give unique keys. It will give all keys. For example if there are two keys 'a' you will have the key 'a' in your set, but it's not unique –  Andrew Jul 19 '12 at 6:18
@Andrew set will only keep unique elements. It will give unique keys. –  Fiktik Jul 19 '12 at 6:19
+1 as I just wrote using a set as a comment in the other answer! ;) –  Mare Infinitus Jul 19 '12 at 6:20
@Andrew on edit: unique means one of each kind. If there are two 'a' keys in multimap, then unique set must contain one 'a' –  Fiktik Jul 19 '12 at 6:21
I agree that both interpretations are possible. Sorry, can't remove -1 until answer is edited –  Andrew Jul 19 '12 at 6:29

I think you can do something like this in case by unique you mean the key that is contained in the multimap only once:

1) construct a sorted list of all keys in your map

2) iterate over the list and find unique keys. It's simple since all duplicates will be near each other in a sorted container

If you want just all keys - use std::set as Donotalo suggested

share|improve this answer
why use a sorted list and not just a set? the set will already prove uniqueness. –  Mare Infinitus Jul 19 '12 at 6:19
@MareInfinitus: because if there are two keys 'a' in the multimap the key 'a' will be in the set, but it's not unique –  Andrew Jul 19 '12 at 6:20
a is a key, even if it is used multiple times, it is still a key that has to be stored. At least I understand the question as not wanting a list of keys that are not used multiple times, but a list of keys. –  Mare Infinitus Jul 19 '12 at 6:22

Your Answer


