Please help me to sort multimap by the count of values.
For example,
multimap<char,int> mymultimap;
mymultimap.insert (pair<char,int>('a',100));
mymultimap.insert (pair<char,int>('a',101));
mymultimap.insert (pair<char,int>('a',111));
mymultimap.insert (pair<char,int>('b',200));
mymultimap.insert (pair<char,int>('b',211));
mymultimap.insert (pair<char,int>('c',300));
Should be after sorting in the following order:
c
b
a
I tried to use custom comparer like this:
struct comparer
{
bool operator() (const char& first, const char& second) const
{
return mymultimap.count(first) < mymultimap.count(second);
}
};
But I can`t define
multimap<char, int, comparer> mymultimap;
Before the struct comparer was defined.
This is not possible.
Unless you go ahead and create a custom structure, it would seem that you need one structure to keep trace of all the items (pairs here), and one to actually count them.
The actual choice mainly depends if the sort operation is a one-off computation or if you actually need to maintain both synchronized.
If it is a one-off computation, then just count when you need and be done with it.
If you need to maintain this order, then I would suggest reading on Boost.MultiIndex and using it as a base to develop a custom class.