在C++中使用向量设置并集算法

Arc*_*ect 4 c++ algorithm vector union-find

我只是std::vector在这个问题中使用,我可以保证每个向量中没有重复(但每个向量中没有任何顺序).我如何结合我的载体?

例:

如果我有以下矢量......

1
1
3 2
5
5 4
2
4
4 2
Run Code Online (Sandbox Code Playgroud)

在结合之后,我应该只剩下两个向量:

1
2 3 4 5
Run Code Online (Sandbox Code Playgroud)

我再次使用矢量,std::set是不允许的.

shi*_*mar 14

您可以使用std :: set_union算法.

int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  std::vector<int> v(10);                      // 0  0  0  0  0  0  0  0  0  0
  std::vector<int>::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
Run Code Online (Sandbox Code Playgroud)

参考:http://www.cplusplus.com/reference/algorithm/set_union/