Chaining of ordering predicates (e.g. for std::sort)

前端 未结 6 1902
说谎
说谎 2021-02-05 23:33

You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted.

Howeve

6条回答
  •  甜味超标
    2021-02-06 00:34

    You can try this:

    Usage:

    struct Citizen {
        std::wstring iFirstName;
        std::wstring iLastName;
    };
    
    ChainComparer cmp;
    cmp.Chain( boost::bind( &Citizen::iLastName, _1 ) );
    cmp.Chain( boost::bind( &Citizen::iFirstName, _1 ) );
    
    std::vector vec;
    std::sort( vec.begin(), vec.end(), cmp );
    

    Implementation:

    template 
    class ChainComparer {
    public:
    
        typedef boost::function TComparator;
        typedef TComparator EqualComparator;
        typedef TComparator CustomComparator;
    
        template