小编Sum*_*gra的帖子

如何在不重载 `operator()`、`std::less`、`std::greater` 的情况下为 `std::multiset` 提供自定义比较器?

我想要一个用于以下代码的自定义比较器。但是,我不允许重载 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";
} …
Run Code Online (Sandbox Code Playgroud)

c++ lambda multimap custom-compare c++11

4
推荐指数
1
解决办法
1071
查看次数

标签 统计

c++ ×1

c++11 ×1

custom-compare ×1

lambda ×1

multimap ×1