STL set_union and set_intersection on unsorted vectors

前端 未结 3 1454
醉梦人生
醉梦人生 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 03:02

    Because it is a requirement for std::set_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

    (emphasis mine)

    you shouldn't call these algorithms on unsorted ranges.

提交回复
热议问题