STL set custom sort

前端 未结 4 523
遥遥无期
遥遥无期 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:16

    Your two sets have different types (the compare function is part of the type definition).

    Since std::set does not provide a constructor or assignment operator for different compare functions, you have to either initialize the new set with an iterator pair to your first set or std::copy over the elements.

    That is:

    set p2d_set2 { std::begin(p2d_set), std::end(p2d_set) };
    

    or

    set p2d_set2;
    std::copy(std::begin(p2d_set), std::end(p2d_set), std::inserter(p2d_set2));
    

提交回复
热议问题