1

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

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

有什么建议么?在此先感谢和亲切的问候。


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;
4

1 回答 1

3

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;
于 2010-07-15T20:42:18.313 回答