c++使用带有自定义类对象的列表容器,list::sort函数实际上并不对我的数据进行排序

标签 c++ sorting

我有一个节点对象的列表容器,它有一个定义的比较操作(见头文件)

节点.h

class Node{

private:

    int xCoord;
    int yCoord;
    int value;
    double fCost;
    double gCost;
    double hCost;
    Node* parent;

public:

    Node();
    Node(int x, int y, int value, int cost, Node* parent);
    void setParent(Node* parent);
    int getX();
    int getY();
    int getValue();
    double getHCost();
    double getFCost();
    double getGCost();
    Node* getParent();
    void setHCost(double hCost);
    bool operator < (Node& rhs)
    {
        return fCost < rhs.fCost;
    }

};

现在,我将我的列表定义为:

list<Node> openList;

    vector<Node> closedList;

    Node *start = initiateStart(map);
    //openList.push_front(*start);
    Node *end;

    Node *temp = new Node(1,2,8, 12, start);
    temp->setHCost(123.2);
    cout << "temp gcost : " << temp->getGCost() <<endl;
    cout << "temp hcost : " << temp->getHCost() <<endl;
    cout << "temp fcost : " << temp->getFCost() <<endl;

    openList.push_front(*temp);

    Node *temp2 = new Node(1,2,8, 23, start);
    temp2->setHCost(123.2);
    cout << "temp2 gcost : " << temp2->getGCost() <<endl;
    cout << "temp2 hcost : " << temp2->getHCost() <<endl;
    cout << "temp2 fcost : " << temp2->getFCost() <<endl;

    openList.push_front(*temp2);

    Node *temp3 = new Node(1,2,8, 1, start);
    temp3->setHCost(123.2);
    cout << "temp3 gcost : " << temp3->getGCost() <<endl;
    cout << "temp3 hcost : " << temp3->getHCost() <<endl;
    cout << "temp3 fcost : " << temp3->getFCost() <<endl;

    openList.push_front(*temp3);

    openList.sort();

    for (list<Node>::iterator iter = openList.begin(); iter != openList.end(); ++iter){

        cout << "iter Fcost : " << iter->getFCost() <<endl;

    }
    }

现在我的程序打印:

temp gcost : 12
temp hcost : 123.2
temp fcost : 135.2
temp2 gcost : 23
temp2 hcost : 123.2
temp2 fcost : 146.2
temp3 gcost : 1
temp3 hcost : 123.2
temp3 fcost : 124.2
iter Fcost : 124.2
iter Fcost : 146.2
iter Fcost : 135.2

但我期望的结果是:

temp gcost : 12
    temp hcost : 123.2
    temp fcost : 135.2
    temp2 gcost : 23
    temp2 hcost : 123.2
    temp2 fcost : 146.2
    temp3 gcost : 1
    temp3 hcost : 123.2
    temp3 fcost : 124.2
    iter Fcost : 124.2
    iter Fcost : 135.2
    iter Fcost : 146.2

根据我的阅读,list::sort 应该使用 define 运算符来执行排序吗?如果是,为什么不排序?

干杯, 克里斯。

最佳答案

我设法通过定义这个来解决这个问题:

typedef struct MyClassComparator {
    bool operator()(const Node& first, const Node& second) {

        //the comparison you want e.g. first fCost < second fCost etc

    }
};

然后母鸡排序说:

openList.sort(MyClassComparator());

关于c++使用带有自定义类对象的列表容器,list::sort函数实际上并不对我的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22355518/

相关文章:

c - 如何顺序修改数组中的值

c++ - 集成库导致错误 C++

c++返回已删除对象的方法

c++ - 数据结构 - 选择排序方法

r - 在 ggplot2 中的 x 轴上绘制有序因子

c++ - 为什么std::unique调用std::sort?

javascript - 根据日期属性对 javascript 对象进行排序

c++ - 有没有办法避免以下代码中的内存泄漏?

c++ - 制作纯虚函数 noexcept 的好的或坏的做法

c++ - QVector<QVector int>>修改 "inside"vector