gpt4 book ai didi

c++ - 当类被 shared_ptr 包装时,std::min 元素出现奇怪的行为

转载 作者:行者123 更新时间:2023-11-30 01:35:14 26 4
gpt4 key购买 nike

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

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

检查一下:

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在一个 vector 中。这是我的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。猜猜哪个被选为 min_element。 -> 第二个!我不知道为什么而且很生气,所以我写了一个简单的 lambda 来比较节点:

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

繁荣,这个:

enter image description here

改为:

enter image description here

很明显,你可以看到第一个是错误的。而且我检查了很多次,它现在一直运行良好,所以问题真的在这里。

所以我的问题是为什么当我使用 std::min_element 时它不起作用.它是否与我拥有 std::vector 的事实有关?的 std::shared_ptr<node> s 而不仅仅是 node秒?我必须写 operator< 吗?在 node类(class)不同?

最佳答案

C++ 文档非常清楚为什么会出现此问题:

如果您查看 shared_ptr 上的页面:

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

Note that the comparison operators for shared_ptr simply compare pointer values; the actual objects pointed to are not compared. Having operator< defined for shared_ptr allows shared_ptrs to be used as keys in associative containers, like std::map and 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 类型。因此,您应该预料到您对数据所做的所有操作都会反射(reflect)出这一点。例如,应用于 shared_ptr 节点的 sizeof() 运算符将返回 shared_ptr 的大小而不是节点的大小。同样,当您对两个 shared_ptr 进行比较时,它是为该类型定义的 shared_ptr 比较运算符。

关于c++ - 当类被 shared_ptr 包装时,std::min 元素出现奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com