Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile std::map sorting, why?

Tags:

c++

This is my code:

map<string, int> errs;
struct Compare {
    bool operator() (map<string, int>::const_iterator l, 
        map<string, int>::const_iterator r) { 
        return ((*l).second < (*r).second); 
    }
} comp;
sort(errs.begin(), errs.end(), comp);

Can't compile. This is what I'm getting:

no matching function for call to ‘sort(..’

Why so? Can anyone help? Thanks!

like image 978
yegor256 Avatar asked Nov 28 '22 23:11

yegor256


1 Answers

You can't sort a map. It has its own sort order, defined at construction time either as the default (use <) or a passed in comparator.

like image 197
Marcelo Cantos Avatar answered Dec 10 '22 07:12

Marcelo Cantos