c++ - 推力 set_intersection 是如何工作的?

标签 c++ algorithm intersection thrust set-intersection

我想知道如何 thrust::set_intersection有效,但从我的测试结果来看,我对这个函数的作用更加困惑。

举几个例子:

const int size1 = 5;
const int size2 = 5;
int A1[size1] = { 2, 3, 4, 5, 6 };
int A2[size2] = { 1, 2, 3, 4, 5 };
int *result = new int[1000];
int *result_end = thrust::set_intersection(A1, A1 + size1, A2, A2 + size2, result, thrust::less<int>());

返回 2, 3, 4, 5

thrust::equal_to<int>()

返回 2, 3, 4, 5, 6

thrust::greater<int>()

什么都不返回

我明白什么是默认set_intersection确实并且我同意结果,但是对于任何其他示例我完全迷失了结果的来源?或者它是如何计算的?

是否知道该算法的工作原理?谁能解释一下?

编辑:

我的目标是给定 2 组元组(假设大小为 2):

A={(1, 1), (2, 2), (3, 3)}
B={(0, 2), (2, 2), (3, 3)}

所以我想在像 >< 这样的元组上定义 on 运算符,它返回满足运算符的所有元素:

><定义为 a.first > b.first && a.second < b.second

所以答案只有 A[0] 和 B[0]。

所以你不能用set_intersection来实现这个对吧?

编辑答案: 没关系,我找到了答案here根据这些规则,此类运算符将不是“严格弱排序”运算符。

最佳答案

需要根据最后一个参数、比较器对两个输入集进行排序。

  • 在第一个示例中,函数正常运行。

  • 集合未根据 thrust::greater<int>() 排序.由于不满足先决条件,该功能无法完成其工作。

  • thrust::equal_to<int>()甚至不是有效的比较器。

关于c++ - 推力 set_intersection 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075778/

相关文章:

c++ - 没有得到 ASCII 数字 =7 的字符串打印输出

c++ - 比较运算符的复杂性

c++ - 嵌套通用容器迭代 C++

algorithm - 根据内部 5x5 值填充 7x7 网格

python - 如何在 python 中正确实现具有 n 个分支因子的树?

arrays - Swift Array 按属性交集

C# linq 距特定路线最近的点

c++ - 如何正确删除一行控件并在该位置动态创建一个新控件?

visual-studio-2008 - 同时连接,excel

php - 反转 switch case 语句(直到为真)