leetcode 621. 任务调度器
先贴出错内容
vector容器和自定义比较函数
vector<pair<char, int>> vp(26);//容器
//自定义比较函数
static bool mycmp(pair<char, int> &a, pair<char, int> &b)
{
return a.second >= b.second;
}
//调用sort
sort(vp.begin(), vp.end(), mycmp);
The comparison function must meet the requirements of Compare concept. One of the requirements is:
If comp(a,b)==true then comp(b,a)==false
Your function violates this requirement, so the program has undefined behaviour.
本例中自定义函数 return a.second >= b.second
会导致comp(a,b)==true 并且comp(b,a)==true,不符合上述原则。
因此需要修改为:
static bool mycmp(pair<char, int> &a, pair<char, int> &b)
{
return a.second > b.second;
}