C++ 包括';设置';a'的内部;结构';
我试图在结构中包含一个集合,但我不知道在执行此操作时如何将回调比较函数传递给集合构造函数 这是我尝试过的一个基本示例:C++ 包括';设置';a'的内部;结构';,c++,callback,struct,set,C++,Callback,Struct,Set,我试图在结构中包含一个集合,但我不知道在执行此操作时如何将回调比较函数传递给集合构造函数 这是我尝试过的一个基本示例: struct pointT { int x; int y; }; struct pathT{ Stack<pointT> pointsInPath; Set<pointT> pointsIncluded; // need callback here? }; // Tried this. //struct path
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 pointT{
int x;
int-y;
};
结构路径{
堆栈点路径;
Set pointsIncluded;//这里需要回调吗?
};
//试过这个。
//结构路径{
//堆栈点路径;
//设置点包括(比较点);//当然不起作用
//};
//用于比较点集的回调函数。
整数比较点(第一点,第二点){
if(firstPoint.x==secondPoint.x&&firstPoint.y==secondPoint.y)返回0;
if(firstPoint.x
使用自定义默认构造函数:
struct pathT{
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded; // need callback here?
pathT() : pointsIncluded(ComparePoints) { }
};
使用自定义默认构造函数:
struct pathT{
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded; // need callback here?
pathT() : pointsIncluded(ComparePoints) { }
};
C++中的结构是一个自动的类。
因此,您可以提供一个构造函数struct pathT {
public:
pathT();
private:
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded;
};
pathT::pathT()
: pointsIncluded(ComparePoints)
{
}
结构路径{
公众:
pathT();
私人:
堆栈点路径;
包括设定点;
};
pathT::pathT()
:包括的点(比较点)
{
}
关于 C++中的结构是一个自动的类。 因此,您可以提供一个构造函数
struct pathT {
public:
pathT();
private:
Stack<pointT> pointsInPath;
Set<pointT> pointsIncluded;
};
pathT::pathT()
: pointsIncluded(ComparePoints)
{
}
结构路径{
公众:
pathT();
私人:
堆栈点路径;
包括设定点;
};
pathT::pathT()
:包括的点(比较点)
{
}
关于我不知道如何实施。你能提供一个简短的例子吗?@joeh100:我已经编辑了我的答案好几次了,但每个版本都提供了与解释相匹配的代码。我不确定你还需要什么。我想我对编辑感到困惑。您提供的第一个示例不应该使用相同的名称来表示pointsIncluded而不是setOfPoints吗?我不知道如何实现它。你能提供一个简短的例子吗?@joeh100:我已经编辑了我的答案好几次了,但每个版本都提供了与解释相匹配的代码。我不确定你还需要什么。我想我对编辑感到困惑。您提供的第一个示例不应该为pointsIncluded使用相同的名称而不是Set of Points吗?