【发布时间】:2020-06-12 21:58:15
【问题描述】:
我想在两个单独的函数中按 key 和 value 对 std::list<std::pair<string, int>> 进行排序。
我收到一条错误消息:
error: reference to non-static member function must be called
sort(test.begin(), test.end(), sortByVal);
代码
class Test
{
std::list<pair<std::string, int>> test;
public:
void sortbykey()
{
sort(test.begin(), test.end(), sortByVal);
}
bool sortByVal(const std::pair<std::string, int>& a, const std::pair<std::string, int>& b)
{
return (a.first < b.first);
}
};
【问题讨论】:
-
请不要描述代码,而是提供minimal reproducible example。如果你对类成员有问题,把他们放在代码中的一个类中。
-
这能回答你的问题吗? why sort function of STL is not working?
标签: c++ sorting c++-standard-library std-pair stdlist