Set union algorithm using vector in C++

后端 未结 3 1895
一生所求
一生所求 2021-01-19 05:03

I\'m only using std::vector in this problem, and I can guarantee no duplicates in each vector (but there isn\'t any order in each vector). How do I union the ve

3条回答
  •  太阳男子
    2021-01-19 05:39

    You can use std::set_union algorithm.

    int first[] = {5,10,15,20,25};
      int second[] = {50,40,30,20,10};
      std::vector v(10);                      // 0  0  0  0  0  0  0  0  0  0
      std::vector::iterator it;
    
      std::sort (first,first+5);     //  5 10 15 20 25
      std::sort (second,second+5);   // 10 20 30 40 50
    
      it=std::set_union (first, first+5, second, second+5, v.begin());
                                                   // 5 10 15 20 25 30 40 50  0  0
      v.resize(it-v.begin());                      // 5 10 15 20 25 30 40 50
    

    Refer :http://www.cplusplus.com/reference/algorithm/set_union/

提交回复
热议问题