c++: set
发布于 2024-09-09 07:37:44 字数 216 浏览 2 评论 0 原文

晚上好(取决于您现在在哪里)。 我对排序集的 stl 东西有点困惑...... 我想在我的集合中存储自定义类的指针,并且我希望它们按我自己的排序 标准而不仅仅是指针大小。

任何人都知道如何做到这一点?既然不可能 像operator<(const foo &*rhs, const foo &*lhs){..}; 那样做

有什么建议吗? 预先感谢并致以亲切的问候。

Good Evening (depending on where u are right now).
I am a little confused with the stl stuff for sorted sets...
I want to store pointers of a custom class in my set and I want them to be sorted by my own
criterion and not just the pointer size.

Anyone has an idea how to do this? Since it is impossible
to do it like operator<(const foo &*rhs, const foo &*lhs){..};

Any suggestions?
Thanks in advance and kind regards.

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

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

发布评论

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

评论(1

淡淡的优雅 2024-09-16 07:37:44

std::set 的第二个模板参数是它用于比较的方法。所以你可以这样做:

struct dereference_compare
{
    template <typename T>
    bool operator()(const T* pX, const T* pY) const
    {
        return *pX < *pY;
    }
};

typedef std::set<T*, dereference_compare> set_type;

std::set's second template parameter is the method it uses for comparisons. So you can do something like this:

struct dereference_compare
{
    template <typename T>
    bool operator()(const T* pX, const T* pY) const
    {
        return *pX < *pY;
    }
};

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