0
我試圖在結構中包含一個集合,但是我不知道如何在執行此操作時將回調比較函數傳遞給set構造函數。在'struct'中包含'set'
這是什麼,我已經嘗試了基本的例子:
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;
}
我不知道如何實現這一點。你能提供一個簡短的例子嗎? – joeh100
@ joeh100:我已經編輯了幾次我的答案,但每個版本都提供了與解釋相匹配的代碼。我不確定你還需要什麼。 –
我想我在編輯之間感到困惑。也不應該你提供的第一個例子使用pointsIncluded而不是setOfPoints相同的名稱? – joeh100