How do I initialize a std::set comparator?

…衆ロ難τιáo~ 提交于 2019-12-07 05:36:26

The template parameter needs to be the type of the comparator, not a specific object of that type; you can provide a specific comparator object to be used in the std::set constructor:

typedef std::set<unsigned int, sortSet> TType;

Object o;
TType mySet(sortSet(o));

I am not sure if I understood your actual question, however, the general idiom to use a custom comparator is shown in the following contrived example.

#include <set>

class foo {
  public:
    int i_;
};

struct foo_comparator {
    bool operator()( foo const & lhs, foo const & rhs ) {
        return lhs.i_ < rhs.i_;
    }
};

typedef std::set< foo, foo_comparator > foo_set;

int main() {
  foo_set my_foo_set;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!