我想要以下代码的自定义比较器。但是,我不允许超载 operator()
, std::less
, std::greater
.
我尝试使用 lambda 来实现这一点,但gcc不允许我auto
用作非静态成员。还有其他方法可以使这项工作吗?
#include <iostream>
#include <map>
#include <set>
class Test
{
public:
// bool operator () (const int lhs, const int rhs) { // not allowed
// return lhs > rhs;
// };
using list = std::multiset<int /*, Test*/>;
std::map<const char*, list> scripts;
};
int main()
{
Test t;
t.scripts["Linux"].insert(5);
t.scripts["Linux"].insert(8);
t.scripts["Linux"].insert(0);
for (auto a : t.scripts["Linux"]) {
std::cout << a << std::endl;
}
std::cout << "end";
}
编辑:使用 lambda
class Test
{
public:
auto compare = [] (const int a, const int b) { return a < b;}
using list = std::multiset<int, compare>; //here
std::map<const char*, list> scripts;
};
错误:
'auto' not allowed in non-static class member
auto compare = [] (const int a, const int b) { return a < b;}