简体   繁体   English

布尔运算符重载不起作用

[英]Bool operator overloading not working

Code sample: 代码示例:

#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: 调试器片段:

调试器

I am making a priority queue and I have tried overloading the () operator but for some reason it is not working at all (as seen in the debugger). 我正在创建优先级队列,并且尝试重载()运算符,但由于某种原因,它根本无法正常工作(如调试器中所示)。

I have a feeling that something is wrong with the pointers but I can't figure out exactly what. 我感觉指针出了点问题,但我无法弄清楚到底是什么。

I don't see a problem in your code. 我看不到您的代码中的问题。 Except that you forget to initialize the random numbers generator. 除非您忘记初始化随机数生成器。

#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;
}

It outputs: 它输出:

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

Which is expected. 这是预期的。 Maybe you get confused because it does not sort items. 也许您会感到困惑,因为它不对项目进行排序。 But it does not have to. 但这不是必须的。 Only the first (top) one has to be grater than all others (according to the comparator). 根据比较器,只有第一个(最上面的)必须比所有其他都更磨碎。 Rest of elements do not follow the n > n+1 . 其余元素不遵循n > n+1 It 's becasue priority qeue is implemented on a heap. 因为优先级qeue是在堆上实现的。 It can operate faster this way. 这样可以更快地运行。 as inserting elements takes O(log n) time. 因为插入元素需要O(log n)时间。 Keeping all elements sorted would require O(n) insert operation. 保持所有元素排序将需要O(n)插入操作。

std::priority_queue is implemented over a heap, which comes with a slightly counter-intuitive side-effect because it's not using a "min heap", so the items are ordered from highest to lowest . std::priority_queue是在堆上实现的,由于没有使用“最小堆”,因此具有有点反直觉的副作用,因此,这些项最高到最低排序

In a nutshell, you have your comparison operator the wrong way around -- if you want your priority queue to be in descending order, you need to test rhs < lhs rather than the other way around. 简而言之,您使比较运算符走错了方向-如果您希望优先级队列按降序排列,则需要测试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;
}

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

383
777
793
886
915

---- ADDENDUM ---- ----附录----

Remember that the underlying std::vector is being used to implement a heap , so the items are not going to appear in-order in the vector when viewed thru the debugger. 请记住,底层的std::vector被用来实现一堆 ,因此当通过调试器查看时,这些项不会在向量中按顺序出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM