C++:如何在包含用户定义结构的两个向量上使用set_intersection
C++: How to use set_intersection on two vectors containing user-defined structs?
我有两个向量,其中充满了非常简单的结构:
typedef struct{
//three vertex ids
uint a,b,c;
} Face;
我目前正在尝试运行set_intersection,如下所示:
set_intersection(listOfFaces1.begin(),listOfFaces1.end(),listOfFaces2.begin(),listOfFaces2.end(), back_inserter(facesToDelete));
我想我需要以某种方式覆盖一些比较器?但我不知道如何定义两个Face对象之间的相等。。。
任何帮助都将不胜感激。
首先,当你用C++编程时,你可以使用:
struct Face {
uint a,b,c;
};
这里有一个实现operator<
的简单策略,适用于标准库中的算法和容器。
struct Face {
uint a,b,c;
bool operator<(Face const& rhs) const
{
if ( a != rhs.a )
{
return ( a < rhs.a);
}
if ( b != rhs.b )
{
return ( b < rhs.b);
}
return ( c < rhs.c);
}
};
或者,正如@Praetorian所建议的,
struct Face {
uint a,b,c;
bool operator<(Face const& rhs) const
{
return std::tie(a, b, c) < std::tie(rhs.a, rhs.b, rhs.c);
}
};
相关文章:
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 查找两个排序向量中共有的元素
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 如何根据两个因素组织向量:id 和数量?(C++)
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 检查两个向量是否并行的最有效方法
- 有没有办法将两个或多个不同的类链接到一个类中(稍后在向量上使用)?
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 如何获得比较两个向量对的子集
- C++将两个不同类型的向量的属性连接到新的向量中
- 如何组合两个整数向量
- 在两个类实例之间共享向量
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- 为什么具有两个元素的发起器语法将一个元素而不是两个元素放入字符串向量中?