C++如何使用 lambda 函数对 unique_ptrs 向量进行动态排序?

发布于 2024-12-05 20:04:40 字数 1215 浏览 0 评论 0 原文

所以我有一个 std::vector> vec 并且我正在尝试对其进行动态排序,因为 Derived1 与 Derivedn 之间存在逻辑比较(Derived1 始终 > Derivedn-1 > ... > Derived1)(假设 n = 10 左右)每个 Derivedx 与 Derivedx 都有自己不同的比较。举个例子,认为 10 位整数 > 9位整数> 1 位整数,但在每个派生类中 53 > 32(但我没有对整数进行排序)。

所以我可以这样做:

std::sort(vec.begin(), vec.end(), 
[](std::unique_ptr<Base>& const a, std::unique_ptr<Base>& const b){
  return *a<*b;}

然后在 Base 中,有一个函数 Base::operator<(const Base& b) 进行比较,如果它们是不同的 Derived 类,如果它们相同,则转换为 Derivedx如果它们是相同的派生,则使用 Derivedx::operator<(const Derivedx& d)

但是,我认为有一种方法可以在给定派生类中的适当定义的情况下自动比较 a 和 b,但由于编译错误,我无法实现它。我无法让 lambda 函数来比较 Derivedx <动态推导。

我尝试过 Base::operator<(const std::unique_ptr) ,然后使用 return *a 出现编译器错误,说我使用了删除的复制赋值运算符(我不明白,赋值在哪里??)。抽象的虚拟 Base::operator<(const Base& b) 与我现在所做的几乎相同,但需要做更多工作,因为我必须实现 Derivedx::operator<(const Base& b) ; b) (对于每个 Derivedx),如果它们相同,则向下转换为 (Derivedx)。

不过,我比较基类中的所有内容可能比在(n 个派生类中的 n 个比较)中实现 n^2 次比较更好。但我确实想看看我是否可以保持“面向对象”。

对设计问题有什么想法吗?
谢谢。

So I have a std::vector<std::unique_ptr<Base>> vec and I'm trying to sort it dynamically, given that there are logical comparisons between Derived1 to Derivedn (Derivedn always > Derivedn-1 > ... > Derived1) (say n = 10 or so) and each Derivedx has it's own different comparison with Derivedx. As an example, think 10 digit integer > 9 digit integer > 1 digit integer, but within each derived class 53 > 32 (but I'm not sorting integers).

So I can do this:

std::sort(vec.begin(), vec.end(), 
[](std::unique_ptr<Base>& const a, std::unique_ptr<Base>& const b){
  return *a<*b;}

And then in Base, have a function Base::operator<(const Base& b) make comparisons if they are different Derived classes, and cast to Derivedx if they are the same with Derivedx::operator<(const Derivedx& d) if they are the same derived.

However, I would think that there's a way that I can compare a to b automatically given the appropriate definitions in the derived classes, but I have been unable to implement it due to compile errors. I cannot get the lambda function to compare Derivedx < Derivedy dynamically.

I've tried Base::operator<(const std::unique_ptr<Base>) and then use return *a<b for a compiler error, saying that I used a deleted copy assignment operator (which I don't understand, where is the assignment??). An abstract virtual Base::operator<(const Base& b) does practically the same thing I'm doing now with more work because I have to implement Derivedx::operator<(const Base& b) (for each Derivedx) and then cast down to (Derivedx) if they're the same.

It may just be better that I compare everything in the base class rather than implementing n^2 comparisons in (n comparisons in n derived classes) though. But I do want to see if I can keep things "object oriented".

Any thoughts on the design issue?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2024-12-12 20:04:40

请看一下 Scott Meyers 的《更有效的 C++》中的第 31 章,使函数对于多个对象而言是虚拟的

另外,尝试用谷歌搜索短语双重调度多重调度

Take a look at Chapter 31, Making functions virtual with respect to more than one object in Scott Meyers, More Effective C++.

Also, try googling on the phrases double dispatch and multiple dispatch.

情释 2024-12-12 20:04:40

嗯...我可能会使用一组相关变体来覆盖 operator< 。这将独立于任何类层次结构。但也许这不是你想要的。

Hmm... I probably would have used overriding the operator< with the set of relevant variants. This would then be independent from any class hierarchy. But maybe this is not what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文