我有一个班级说class stuff{ int id; int ammount; int quality; /*...*/ };
,我有一个vector
.我想让我的收藏品可以排序stuff::id
和搜索staff::id
.我可以通过find_if来完成它并使用一些特殊的基于stl lambda的排序函数进行排序.然而,我想在向量中默认使用它.我听说有一些方法来创建散列函数,但我搜索周围找不到它...那么如何使用默认的向量函数使类可排序/可搜索内部向量?
对于排序,实现一个operator<
比较两个结构:
bool operator<(const stuff& s1, const stuff& s2) { // Your comparison here }
这是默认情况下用于大多数排序的运算符.
对于相等性检查,请创建operator==
类似的.