How to create a std::set with custom comparator in C++?

前端 未结 2 1525
野的像风
野的像风 2021-01-15 20:15

How do I create a set of pairs, the elements of which (the pairs) are sorted with a custom bool function? I write

set ,compare> my         


        
2条回答
  •  清歌不尽
    2021-01-15 20:59

    You should use a functional object. Here is an example

    #include 
    #include 
    #include 
    
    struct Compare
    {
        bool operator ()( const std::pair &p1, 
                          const std::pair &p2 ) const
        {
            return ( p1.second - p1.first  > p2.second - p2.first );
        }
    };
    
    int main() 
    {
        std::set, Compare> s;
    
        return 0;
    }
    

提交回复
热议问题