is there an iterator across unique keys in a std::multimap?
c++, multimap
Solution
You can use `upper_bound` to increment the iterator position instead of `++`:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
multimap<int,string> mm;
mm.insert(make_pair(1, "a"));
mm.insert(make_pair(1, "lemon"));
mm.insert(make_pair(2, "peacock"));
mm.insert(make_pair(3, "angel"));
for( auto it = mm.begin(), end = mm.end();
it != end;
it = mm.upper_bound(it->first)
)
cout << it->first << ' ' << it->second << endl;
return 0;
}
This results in:
1 a
2 peacock
3 angel
Problem
Is there a simple or standard way to have a multimap iterator which iterate across unique keys in a multimap? i.e. for a set that looks like: `{1, "a"}, {1, "lemon"}, {2, "peacock"}, {3, "angel"}` an iterator which would start at `{1, "a"}` then incrementing would point to `{2, "peacock"}` and then incrementing again would point to `{3, "angel"}`?