gpt4 book ai didi

c++ - 将 2 个双数组组合成一个找到它们的并集和交集的最简单方法

转载 作者:行者123 更新时间:2023-11-30 03:55:49 27 4
gpt4 key购买 nike

我一直在努力完成这件事,但现在卡住了。谁能推荐最简单的方法来完成它?谢谢

c[i] = a[i] + b[i];

// The snippet of code I have above works fine with operator: + - / *
// a[i] = { 1, 8, 13, 15 }
// b[i] = { 3, 8, 13, 20 }
// then c[i] = { 4, 16, 26, 35 }

// But how do i find the union and intersection of both arrays?
// I want the results to look like:
// union = {1,3,8,13,15,20}
// intersection = {8,13}

最佳答案

既然你问的是最简单的方法。您可以将数组的元素放入 2 std::set

std::set<T> set1(begin(array1), end(array1));
std::set<T> set2(begin(array2), end(array2));

然后在 2 个新的 std::set 中使用 std::set_union 和 std::set_intersection。请查看其在这些链接中的完成方式 set unionset intersection因此,STL 负责大部分逻辑和容器。

关于c++ - 将 2 个双数组组合成一个找到它们的并集和交集的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com