当类由shared_ptr包装时,std :: min元素怪异的行为

时间:2019-02-08 06:09:29

标签: c++ stl operator-overloading

我进行了很长时间的调试(6小时或更长时间)。我正在调试我的A *算法实现。

在检查完所有可能性之后,在添加日志记录,逐步调试等之后,我终于找到了答案。基本上,所有这些都归结为一行,我在矢量中搜索最小值。

检查一下:

auto open_set = std::vector<std::shared_ptr<node>>{start_node};

std::shared_ptr<node> current;
while (!open_set.empty())
{
    current = *std::min_element(open_set.begin(), open_set.end());

应该在current = *std::min_element(open_set.begin(), open_set.end());行中找到向量中最低的node。这是我的node实现:

class node
{
public:
    node() : G(0), H(0) {}
    node(const QPoint& p) : pos(p), G(0), H(0) {}
    bool operator==(const node& o) const { return pos == o.pos;}
    bool operator==(const QPoint& o) const { return pos == o; }
    bool operator!=(const node& o) const { return pos != o.pos; }
    bool operator<(const node& o) const { return  G + H < o.G + o.H; }
    QPoint pos;
    std::shared_ptr<node> parent;
    int G;
    int H;
};

因此,我拥有搜索operator<所需的min_element。问题是,在多次查看日志后,我发现我有一个node,即G = 8,H = 10,节点G = 10,H = 10。 1}}->第二个!我不知道为什么,而且很生气,所以我写了一个简单的lambda来比较节点:

min_element

蓬勃发展,这:

enter image description here

已更改为:

enter image description here

很明显,您可以看到第一个错误。而且我检查了很多遍,它现在一直可以正常工作,所以问题出在这里。

所以我的问题是,为什么我在使用current = *std::min_element(open_set.begin(), open_set.end(), [&] (const std::shared_ptr<node>& lhs, const std::shared_ptr<node>& rhs) { return lhs->G + lhs->H < rhs->G + rhs->H; }); 时不起作用。它是否与我有std::min_element的{​​{1}}而不仅仅是std::vector的{​​{1}}有关?我必须在std::shared_ptr<node>类中写node吗?

1 个答案:

答案 0 :(得分:4)

关于发生此问题的原因,C ++文档非常清楚:

如果您查看shared_ptr上的页面:

https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_cmp

  

请注意,shared_ptr的比较运算符只是比较   指针值;指向的实际对象不进行比较。有   为shared_ptr定义的operator <允许将shared_ptrs用作键   在关联容器中,例如std :: map和std :: set。

但是有一种方法可以使std :: min达到您想要的行为。您可以实现比较功能对象,也可以像以前一样使用lambda。

class node
{
public:
    node() : G(0), H(0) {}
    node(int x, int y) : G(x), H(y) {}

    bool operator<(const node& o) const { return  (G + H) < (o.G + o.H); }

    int G;
    int H;
};

struct NodeComparer
{
    bool operator()(std::shared_ptr<node> const& lhs, std::shared_ptr<node>  const& rhs) const
    {
        return *lhs < *rhs;
    }
};

int main()
{
    std::shared_ptr<node> a = std::make_shared<node>(3, 6);
    std::shared_ptr<node> b = std::make_shared<node>(1, 1);
    std::shared_ptr<node> c = std::make_shared<node>(2, 2);

    auto open_set = std::vector<std::shared_ptr<node>>
    { 
        a,b,c
    };

    std::shared_ptr<node> current;

    current = *std::min_element(open_set.begin(), open_set.end(), NodeComparer());


    getchar();

}

将节点包装在shared_ptr中后,您将不再处理该节点类型,而是在处理shared_ptr类型。因此,您应该预期对数据执行的所有操作都会反映出这一点。例如,应用于shared_ptr节点的sizeof()运算符将为您提供shared_ptr的大小,而不是节点的大小。 同样,当您对两个shared_ptr进行比较时,就是为该类型定义的shared_ptr比较运算符。