set_union 使用数组时出现问题

set_union issue while using arrays

我正在尝试使用 set_union 获得 4 个数组的并集。这是我目前的代码:

int setA[5] = {2, 4, 5, 7, 8};
int setB[7] = {1, 2, 3, 4, 5, 6, 7};
int setC[5] = {2, 5, 8, 8, 15};
int setD[6] = {1, 4, 4, 6, 7, 12};

int AunionB[12];
int CunionD[11];
int finalUnion[23];

int *lastAunionB;
int *lastCunionD;

ostream_iterator<int> screen(cout, " ");

lastAunionB = set_union(setA, setA+5, setB, setB+7, AunionB);

cout << "AunionB = ";
copy(AunionB, lastAunionB, screen);
cout << endl;

lastCunionD = set_union(setC, setC+5, setD, setD+6, CunionD);

cout << "CunionD = ";
copy(CunionD, lastCunionD, screen);
cout << endl;

set_union(AunionB, AunionB+12, CunionD, CunionD+11, finalUnion);

cout << "Final Union = ";
copy(finalUnion, finalUnion+23, screen);
cout << endl;

当我 运行 代码时,我得到以下输出:

AunionB = 1 2 3 4 5 6 7 8 
CunionD = 1 2 4 4 5 6 7 8 8 12 15 
Final Union = 1 2 3 4 5 6 7 2 4 4 5 6 7 8 8 12 15 52187240 1 1863041424 32767 0 0 

因此,setA and setB 的并集与 setC and setD 的并集一样按预期工作。但是,当我尝试获取所有集合的并集时,它不起作用!我猜 finalUnion 的最后 5 个值是地址字段,但如何删除它们?另外,union 本身是不正确的,我不明白为什么。

Union 操作删除了两个集合共有的值。

请注意,AUnionB 有 8 个元素(不是您的代码预测的 12 个)。

您需要调整联合代码以说明两个初始联合的实际大小。您已准备好正确执行此操作:

int *lastFinalUnion = set_union(AunionB, lastAunionB, CunionD, lastCunionD, finalUnion);

请注意,集合 C 有两次不同的 8,集合 D 有两次不同的 4,这就是它们在中间结果中出现重复的原因。

更新

Also, I tried your code and i'm getting the answer as 1 2 3 4 5 6 7 2 4 4 5 6 7 8 8 12 15 . Shouldn't the answer be 1 2 3 4 4 5 6 7 8 8 12 15

我相信你是对的,但我不是在 C++ 编译器前单步执行并查看正在执行的操作,或验证你的输出。实际代码是由另一个 SO 成员编辑的,但对我来说它看起来是正确的。

In the simplest case, set_union performs the "union" operation from set theory: the output range contains a copy of every element that is contained in [first1, last1), [first2, last2), or both. The general case is more complicated, because the input ranges may contain duplicate elements. The generalization is that if a value appears m times in [first1, last1) and n times in [first2, last2) (where m or n may be zero), then it appears max(m,n) times in the output range.

https://www.sgi.com/tech/stl/set_union.html

A​​unionB 和 Cuniond 的大小不是 12 和 11 因为:

Elements from the second range that have an equivalent element in the first range are not copied to the resulting range.

试试这个代码:

int setA[5] = { 2, 4, 5, 7, 8 };
int setB[7] = { 1, 2, 3, 4, 5, 6, 7 };
int setC[5] = { 2, 5, 8, 8, 15 };
int setD[6] = { 1, 4, 4, 6, 7, 12 };

int AunionB[12];
int CunionD[11];
int finalUnion[23];

int *lastAunionB;
int *lastCunionD;

ostream_iterator<int> screen(cout, " ");

lastAunionB = set_union(setA, setA + 5, setB, setB + 7, AunionB);

cout << "AunionB = ";
copy(AunionB, lastAunionB, screen);
cout << endl;

lastCunionD = set_union(setC, setC + 5, setD, setD + 6, CunionD);

cout << "CunionD = ";
copy(CunionD, lastCunionD, screen);
cout << endl;

int *finalUnionEnd;
finalUnionEnd = set_union(AunionB, lastAunionB, CunionD, lastCunionD, finalUnion);

cout << "Final Union = ";
copy(finalUnion, finalUnionEnd, screen);
cout << endl;

然后你得到了正确的结果:

Final Union = 1 2 3 4 4 5 6 7 8 8 12 15