说我有
class newVector: public std::vector<T> {
public:
bool operator< (const newVector& v) {
//..
}
};
和
a std::set<newVector>;
我无法正确使用 a.find(...),我不确定要在 (...) 中放入什么才能使用 newVector::operator<。当我只放置 a.find(element) 时,它使用 std::less。我应该以某种方式更改 std::less 吗?
最佳答案
暂时忽略派生自std::vector
是个坏主意,我可以想到以下方法来解决这个问题:
定义
operator<
对于newVector
的对象.class newVector: public std::vector<T> { public: bool operator< (const newVector& v) const { //.. }
和
std::set<newVector> a; a.find(...);
定义一个具有适当
operator()
的仿函数函数并使用它来创建std::set
.template <typename T> struct NewVectorLess { bool operator()(newVector<T> const& lhs, newVector<T> const& rhs) { // ... } };
和
std::set<newVector<int>, NewVectorLess<int>> a; a.find(...);
关于c++ - 如何将 std::find 与不是 std::less 的 < 运算符一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167529/