STL set custom sort

前端 未结 4 524
遥遥无期
遥遥无期 2021-01-27 17:03

I was trying to resort a set when i realised that it was impossible to resort a set and i had to create a new set and have a custom sort function to resort it . I researched onl

4条回答
  •  遥遥无期
    2021-01-27 17:28

    It's usually better to define a functor class to specify the ordering, rather than a function; that way, you can specify it as a template parameter:

    struct p2d_sortby_y {
        bool operator()(Point2D x, Point2D y) {return whatever;}
    };
    

    This has to take its arguments by value or const reference (not non-const reference, as yours does) to be used on the constant set members.

    You can't directly initialise the new set from a different type of set, but you can initialise it from its range:

    set p2d_set2(p2d_set.begin(), p2d_set.end());
    

提交回复
热议问题