我试图在结构中包含一个集合,但是这样做时我不知道如何将回调比较函数传递给集合构造函数。
这是我尝试过的基本示例:
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;
}
最佳答案
您在c ++中的结构自动是一个类。
因此,您可以提供一个构造函数
struct pathT {
public:
pathT();
private:
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded;
};
pathT::pathT()
: pointsIncluded(ComparePoints)
{
}
问候