#include <iostream>
#include <map>
#include <utility>
#include <algorithm>
int main()
{
std::map<int, std::string> m;
m[2] = "abc";
m[1] = "bcd";
auto cmp = [](std::pair<int, std::string> a, std::pair<int, std::string> b)
{
return a.second != b.second ? a.second < b.second : a.first < b.first;
};
std::sort(m.begin(), m.end(), cmp);
for (auto it = m.begin(); it != m.end(); ++it)
{
std::cout<<it->first<<std::endl;
std::cout<<it->second<<std::endl;
}
return 0;
}
I want to sort a map by its value, instead of its key, so I code as above.
I just read this link and this is why I code like this: std::map, how to sort by value, then by key
But it produced an error:
Severity Code Description Project File Line Suppression State
Error C2784 'unknown-type std::operator -(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::_Tree_unchecked_iterator<_Mytree>' testcpp c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\algorithm 2908
Aucun commentaire:
Enregistrer un commentaire