并集算法下使用矢量++ [英] Set union algorithm using vector in C++
本文介绍了并集算法下使用矢量++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在这个问题上使用的std ::矢量
,我可以保证每个向量没有重复(但没有任何顺序在每个向量) 。我如何结合使我有引导?
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 vectors I have?
例如:
如果我有以下载体...
If I have following vectors...
1
1
3 2
5
5 4
2
4
4 2
工会后,我应该只有两个向量左:
After the union I should have only two vectors left:
1
2 3 4 5
再次声明,我只能用向量,的std ::设为
是不允许的。
推荐答案
您可以使用std :: set_union算法。
You can use std::set_union algorithm.
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
参见:<一href="http://www.cplusplus.com/reference/algorithm/set_union/">http://www.cplusplus.com/reference/algorithm/set_union/
这篇关于并集算法下使用矢量++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文