Bool运算符重载不起作用

时间:2013-11-16 19:19:35

标签: c++ debugging pointers operator-overloading priority-queue

代码示例:

#include <queue>
#include <vector>

using namespace std;

class Cell
{
public:
    int totalCost = 0;
};

class Helper
{
public:
    struct Comparator
    {
        bool operator()(Cell const *lfs, Cell const *rhs)
        {
            return lfs->totalCost < rhs->totalCost;
        }
    };
};

priority_queue<Cell*, vector<Cell*>, Helper::Comparator> path;

void function(Cell* cell)
    {
        cell->totalCost += rand() % 1000;
        path.push(cell);
    }

    int main()
    {
        Helper help;

        Cell first;
        Cell second;
        Cell third;
        Cell fourth;
        Cell fifth;

        Cell* firstPtr = &first;
        Cell* secondPtr = &second;
        Cell* thirdPtr = &third;
        Cell* fourthPtr = &fourth;
        Cell* fifthPtr = &fifth;



        function(firstPtr);
        function(secondPtr);
        function(thirdPtr);
        function(fourthPtr);
        function(fifthPtr);

        return 0;
    }

Debugger snip:

Debugger

我正在建立一个优先级队列,我尝试重载()运算符但由于某种原因它根本不起作用(如调试器中所示)。

我觉得指针有问题,但我无法弄明白究竟是什么。

2 个答案:

答案 0 :(得分:2)

我的代码中没有问题。除了你忘记初始化随机数生成器。

#include <iostream>
#include <queue>
#include <vector>
#include <cstdlib>

using namespace std;

class Cell
{
public:
    int totalCost = 0;
};

class Helper
{
public:
    struct Comparator
    {
        bool operator()(Cell const *lfs, Cell const *rhs)
        {
            return lfs->totalCost < rhs->totalCost;
        }
    };
};

priority_queue<Cell*, vector<Cell*>, Helper::Comparator> path;

void function(Cell* cell)
{
    cell->totalCost += rand() % 1000;
    path.push(cell);
}

int main()
{
    srand(time(0));
    Helper help;

    Cell first;
    Cell second;
    Cell third;
    Cell fourth;
    Cell fifth;

    Cell* firstPtr = &first;
    Cell* secondPtr = &second;
    Cell* thirdPtr = &third;
    Cell* fourthPtr = &fourth;
    Cell* fifthPtr = &fifth;

    function(firstPtr);
    function(secondPtr);
    function(thirdPtr);
    function(fourthPtr);
    function(fifthPtr);

for(;path.size();path.pop()) std::cout << path.top()->totalCost <<"\n";

    return 0;
}

输出:

luk32@debianvm:~/projects/tests$ ./a.out 
896
725
370
200
130
luk32@debianvm:~/projects/tests$ ./a.out 
699
672
285
250
208
luk32@debianvm:~/projects/tests$ ./a.out 
772
582
388
223
153
luk32@debianvm:~/projects/tests$ ./a.out 
869
807
670
642
182

预期。也许你会感到困惑,因为它不会对物品进行排序。但它没有必要。只有第一个(顶部)必须比其他所有(根据比较器)更大。其余元素不遵循n > n+1。这是因为优先级qeue是在堆上实现的。它可以通过这种方式更快地运行因为插入元素需要O(log n)时间。保持所有元素排序需要O(n)插入操作。

答案 1 :(得分:1)

std::priority_queue是在堆上实现的,它带有一个稍微反直觉的副作用,因为它没有使用“min heap”,因此项目从最高到最低排序

简而言之,您的比较运算符是错误的 - 如果您希望优先级队列按降序排列,则需要测试rhs < lhs而不是相反。

#include <queue>
#include <vector>
#include <iostream>

struct Cell
{
    int m_totalCost;
    Cell(int totalCost=0) : m_totalCost(totalCost) {}

    struct PriorityCompare
    {
        bool operator()(Cell const* lhs, const Cell* rhs) const
        {
            return rhs->m_totalCost < lhs->m_totalCost;
        }
    };
};

std::priority_queue<Cell*, std::vector<Cell*>, Cell::PriorityCompare> g_path;

int main()
{
    Cell first(rand() % 1000);
    Cell second(rand() % 1000);
    Cell third(rand() % 1000);
    Cell fourth(rand() % 1000);
    Cell fifth(rand() % 1000);

    g_path.push(&first);
    g_path.push(&second);
    g_path.push(&third);
    g_path.push(&fourth);
    g_path.push(&fifth);

    while (g_path.empty() == false) {
        Cell* cell = g_path.top();
        g_path.pop();
        std::cout << cell->m_totalCost << "\n";
    }
    return 0;
}

现场演示:http://ideone.com/BEjt4R输出

383
777
793
886
915

---- ADDENDUM ----

请记住,基础std::vector用于实现heap,因此当通过调试器查看时,这些项不会在向量中按顺序显示。