晚上好(取决于你现在在哪里)。我对排序集的 stl 东西有点困惑......我想在我的集合中存储自定义类的指针,我希望它们按照我自己的标准进行排序,而不仅仅是指针大小。
任何人都知道如何做到这一点?因为不可能像 operator<(const foo &*_Stack Overflow中文网
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
任何人都知道如何做到这一点?因为不可能像 operator<(const foo &*
任何人都知道如何做到这一点?因为不可能像 operator<(const foo &*rhs, const foo &*lhs){..};
有什么建议么?在此先感谢和亲切的问候。
std::set的第二个模板参数是它用于比较的方法。所以你可以做这样的事情:
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;