STL set_union and set_intersection on unsorted vectors

前端 未结 3 1457
醉梦人生
醉梦人生 2021-01-27 02:16

Below is my code on set intersection and union test that I did. I don\'t understand why it the output is incorrect when I commented out the sort function. Why is the sort necess

3条回答
  •  故里飘歌
    2021-01-27 02:50

    That's required. This is excerpt from documentation of std::union.:-

    Constructs a sorted range beginning at d_first consisting of all elements present in one or both sorted ranges [first1, last1) and [first2, last2).
    1) Expects both input ranges to be sorted with operator<
    2) Expects them to be sorted with the given comparison function comp
    If some element is found m times in [first1, last1) and n times in [first2, last2), then all m elements will be copied from [first1, last1) to d_first, preserving order, and then exactly std::max(n-m, 0) elements will be copied from [first2, last2) to d_first, also preserving order.
    

提交回复
热议问题