Using custom std::set comparator

后端 未结 5 1554
耶瑟儿~
耶瑟儿~ 2020-11-22 16:03

I am trying to change the default order of the items in a set of integers to be lexicographic instead of numeric, and I can\'t get the following to compile with g++:

5条回答
  •  独厮守ぢ
    2020-11-22 16:45

    Yacoby's answer inspires me to write an adaptor for encapsulating the functor boilerplate.

    template< class T, bool (*comp)( T const &, T const & ) >
    class set_funcomp {
        struct ftor {
            bool operator()( T const &l, T const &r )
                { return comp( l, r ); }
        };
    public:
        typedef std::set< T, ftor > t;
    };
    
    // usage
    
    bool my_comparison( foo const &l, foo const &r );
    set_funcomp< foo, my_comparison >::t boo; // just the way you want it!
    

    Wow, I think that was worth the trouble!

提交回复
热议问题