Topological sorting using std::sort

后端 未结 1 885
再見小時候
再見小時候 2020-12-19 17:32

Note: While writing this question, I think I already found the answer. Feel free to ammend or append it with a better version. I thought it might be nice to

相关标签:
1条回答
  • 2020-12-19 17:52

    Consider the values

    pair
        x{0, 1},
        y{2, 0},
        z{1, 2};
    

    Here,

    !tpc(x, y) && !tpc(y, x);
    !tpc(y, z) && !tpc(z, y);
    

    However,

    tpc(x, z);
    

    Thus your comparator does not impose a strict weak ordering, and behavior is undefined if you use it with std::sort or in any other role where a strict weak ordering is required.

    A comparator that is strict weak and is a refinement of your comparator would be:

    struct refined_comparator {
        bool operator()(const pair &p, const pair &q) const { return p.a + p.b < q.a + q.b; }
    } rc;
    
    0 讨论(0)
提交回复
热议问题