使用std::sort对struct object排序

    技术2022-05-20  60

    需要重载运算符'<',如下面的程序:

     

    typedef struct { float w; int a, b; } edge; bool operator<(const edge &a, const edge &b) { return a.w < b.w; }

     

    以后可以调用std::sort根据dge结构体w的值对进行排序,如:

     

    std::sort(edges, edges + num_edges);


    最新回复(0)