使用抽象类进行 STL 排序

发布于 2024-08-09 17:41:17 字数 581 浏览 1 评论 0原文

我在使用 STL 排序函数对派生类进行排序时遇到问题。

示例 -

标头:

vector<AbstractBaseClass *> *myVector;  

在 ImpL 中:

sort(myVector->begin(), myVector->end(), compareBy);

比较器:

bool MyClass::compareBy(AbstractBaseClass& a, AbstractBaseClass& b) {
    return (a->someMethod() < b->someMethod());
}

编辑:这个问题面向使用 STL 对抽象类进行排序的一般用法(我不会发布跟踪转储)。如果它还不明显,我会说它根本不可能按照打印的方式进行编译。相反,我问的是(给定数据结构)人们通常如何对这个玩具抽象类进行排序。

感谢您的快速回答,我相信你们已经搞定了!

我很喜欢 StackOverFlow!

I'm having a problem sorting my derived classes with the STL sort function.

Example -

The header:

vector<AbstractBaseClass *> *myVector;  

In the ImpL:

sort(myVector->begin(), myVector->end(), compareBy);

The comparator:

bool MyClass::compareBy(AbstractBaseClass& a, AbstractBaseClass& b) {
    return (a->someMethod() < b->someMethod());
}

Edit: This question is geared toward general usage of sorting abstract classes with the STL (I'm not posting a trace dump). If its not apparent already, I'll say that there is no way in hell it can compile as printed. Rather, I'm asking (given the data structures) how one would typically sort with this toy abstract class.

Thanks for the quick answers, I believe you guys already nail'd it!

Got'a love StackOverFlow!

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

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

发布评论

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

评论(2

谷夏 2024-08-16 17:41:17

示例:

struct Abstr {
    virtual int some()const == 0;
    virtual ~Abstr() = default;
};

bool abstrSmaller( const Abstr* a1, const Abstr* a2 ) { 
   return a1->some() < a2->some(); 
}

int main() {
   vector<Abstr*> v;

   sort( v.begin(), v.end(), abstrSmaller );
}
  1. 比较函数不应该是成员函数:静态函数或自由函数。
  2. 它应该将向量的元素作为参数,即指针,而不是引用。
  3. 它可以调用 const 函数,因此它可以接受 const 参数。

An example:

struct Abstr {
    virtual int some()const == 0;
    virtual ~Abstr() = default;
};

bool abstrSmaller( const Abstr* a1, const Abstr* a2 ) { 
   return a1->some() < a2->some(); 
}

int main() {
   vector<Abstr*> v;

   sort( v.begin(), v.end(), abstrSmaller );
}
  1. The compare function should not be a member function: either a static or a free function.
  2. It should take the vector's elements as argument, i.e. a pointer, not a reference.
  3. It can call const functions, so it can accept const arguments.
旧夏天 2024-08-16 17:41:17
  1. compareBy 不能是非静态成员函数(因为这需要编译器将隐式 this 指针传递给该函数)。您可以将其设为静态成员、自由函数或函数对象。后者通常更好,因为它通常允许更好的优化(尽管在您的代码中,抽象惩罚可能会撤销这一点)。
  2. 比较必须采用向量的 value_type,这是一个 AbstractBaseClass*,而不是 AbstractBaseClass&

这看起来像下面这样:

struct compareBy : public std::binary_function< const AbstractBaseClass*
                                              , const AbstractBaseClass*
                                              , bool > {
  bool operator()(const AbstractBaseClass* a, const AbstractBaseClass* b) {
    return (a->someMethod() < b->someMethod());
  }
};

您还可以使这个比较器成为您的类的私有成员,这样它就不会污染任何名称空间。

  1. That compareBy must not be a non-static member function (since this would require the implicit this pointer to be passed to the function by the compiler). You can either make it a static member, a free function, or a function object. The latter is generally better, as it usually allows better optimization (although in your code the abstraction penalty might undo that anyway).
  2. The comparison must take your vector's value_type, and that's an AbstractBaseClass*, not an AbstractBaseClass&.

This would look like the following:

struct compareBy : public std::binary_function< const AbstractBaseClass*
                                              , const AbstractBaseClass*
                                              , bool > {
  bool operator()(const AbstractBaseClass* a, const AbstractBaseClass* b) {
    return (a->someMethod() < b->someMethod());
  }
};

You can also make this comparator a private member of your class, so it doesn't pollute any namespace.

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