gpt4 book ai didi

C++ 使用 shared_ptr 但调用对象的关系运算符?

转载 作者:行者123 更新时间:2023-11-28 05:17:46 25 4
gpt4 key购买 nike

我正在尝试编写自己的堆类。我的模板化堆类要求在模板类型上定义运算符“>”和“<”。

当使用我编写的示例类的实例时,一切似乎都运行良好(并且在 int 上也运行良好)。但是,由于类实例从堆中的不同节点移动时存在如此多的实例构造,因此我决定看看在创建类的 shared_ptr 堆时发生了什么。虽然我确实看到构造的实例数量下降了,但堆没有正常工作,因为智能指针“>”和“<”正在被调用,我猜这只是比较智能指针引用。

我想到的一个解决方案是允许比较类型,就像许多 STL 类型所做的那样,这样我就可以将自己的比较类型传递到堆类中,该堆类将取消引用 shared_ptr 并调用操作底层类型。

我在 shared_ptr 上读到的一些文档说它们实现了关系运算符(即 <),因此它们可以用作关联容器中的键。我正在考虑什么时候我可能想要使用 shared_ptr 作为 key 而不是拥有我自己的特定 key 。

我的样本类型堆似乎工作正常:

heap<foo> foo_heap(heap_type::max);
for (unsigned int i = 0; i < 10; ++i)
{
std::string s = "string ";
s += ('0' + i);
foo f(i, s);
foo_heap.push(f);
}
cout << "root: " << foo_heap.top() << endl;

将我的示例类包装在一个不起作用的 shared_ptr 中,例如。就我要实现的目标而言,未满足堆约束。

heap<shared_ptr<foo>> foo_heap_smart(heap_type::max);
for (unsigned int i = 0; i < 10; ++i)
{
std::string s = "string ";
s += ('0' + i);
shared_ptr<foo> f(new foo(i, s));
foo_heap_smart.push(f);
}
cout << "root: " << *(foo_heap_smart.top()) << endl;

我的示例 foo 类:

class foo
{
public:
foo(int value, std::string s) : _value(value), _s(s)
{
std::cout << "foo::foo()" << std::endl;
}

foo(const foo& f) : _value(f._value), _s(f._s)
{
std::cout << "foo::foo(const foo& f)" << std::endl;
}

~foo()
{
std::cout << "foo::~foo()" << std::endl;
}

virtual void operator=(const foo& f)
{
std::cout << "foo::operator=()" << std::endl;
this->_value = f._value;
this->_s = f._s;
}

virtual bool operator<(const foo& right)
{
return this->_value < right._value;
}

virtual bool operator>(const foo& right)
{
return this->_value > right._value;
}

void print(ostream& stm) const
{
stm << "value: " << this->_value << ", s: " << this->_s;
}

private:
int _value;
std::string _s;
};

所以我假设很多人都遇到过类似的问题。只是想知道规定的解决方案是什么。正如我提到的,我想我知道出现的问题可能是一个很好的解决方案,但我想检查一下,因为看起来智能指针可能会因为它们实现关系运算符而导致很多问题。

谢谢,尼克

最佳答案

规定的解决方案是,如果默认版本不适合您的需要,请提供您自己的比较运算符版本。 heap 类的更好设计是也采用 Comparator 类型,它可以默认为 std::less

template <typename T, typename Comp = std::less<T>>
class heap {
...
};

现在为您提供专用于 shared_ptrless 版本。

template <typename T>
struct less<shared_ptr<T>> {
bool operator()(const shared_ptr<T>& a, const shared_ptr<T>& b) const {
*a < *b;
}
};

为了更好的设计,您可以添加一些元编程 hack,使其仅适用于可以比较的类型 T

关于C++ 使用 shared_ptr 但调用对象的关系运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42257333/

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