包括'struct'内部的'set'
Including a 'set' inside of a 'struct'
我试图包括一个集合,内部结构,但我不知道如何传递回调比较函数到集构造函数时这样做。
这是我尝试过的一个基本例子:
struct pointT {
int x;
int y;
};
struct pathT{
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded; // need callback here?
};
// Tried this.
//struct pathT{
//Stack<pointT> pointsInPath;
//Set<pointT> pointsIncluded(ComparePoints); //doesn't work of course
//};
//Callback function to compare set of points.
int ComparePoints(pointT firstPoint, pointT secondPoint){
if (firstPoint.x == secondPoint.x && firstPoint.y == secondPoint.y) return 0;
if (firstPoint.x < secondPoint.x) return -1;
else return 1;
}
int main() {
Set<pointT> setOfPoints(ComparePoints); // this works fine
//pathT allPaths; // not sure how to assign call back function here to a set inside a struct
return 0;
}
使用自定义默认构造函数:
struct pathT{
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded; // need callback here?
pathT() : pointsIncluded(ComparePoints) { }
};
将比较器移到一个结构体中(可以内联,不像函数指针),并将其定义为<
操作符,这是set
所期望的:
struct ComparePoints {
bool operator()(const pointT& a, const pointT& b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
};
struct pathT {
...
pathT() : pointsIncluded(ComparePoints()) { }
};
c++中的结构体自动成为一个类。
因此可以提供构造函数
struct pathT {
public:
pathT();
private:
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded;
};
pathT::pathT()
: pointsIncluded(ComparePoints)
{
}
对
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- C++Union/Struct位域的实现和可移植性
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- 如何使用set实现无序数据结构?
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 使用运算符调用 void 函数时出错<set>
- 修改"std::set"中用户定义类型的值
- 错误:"Left of getValue must have class/struct/union"
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 数据类型"struct seq<0, 1, 2>{}"含义是什么?
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- 包括'struct'内部的'set'