【发布时间】:2019-07-02 17:41:49
【问题描述】:
我需要找到两个具有相同字段的元素。它们应该在同一个向量中是连续的。我需要用 STL 方法来做。我试过使用 find 或 find_if 但做不到。你能给我任何提示吗?
我的代码(部分代码):
class Sound {
private:
int notePitch, length;
public:
Sound(int notePitch, int length) {
this->notePitch = notePitch;
this->length = length;
}
int getPitch() {
std::cout << "Pitch: " << this->notePitch << " length: " << this->length;
return this->notePitch;
}
};
实际查找功能:
std::vector<Sound>::iterator iter = masterpiece.begin();
std::vector<Sound>::iterator iterEnd = masterpiece.end();
std::vector<Sound>::iterator it = find_if(iter, iterEnd, [](auto prev, auto next) -> bool {
return prev.getPitch() == next.getPitch();
});
我得到的错误是这样的:
c2678 binary '==' no operator found which takes a left-hand operand of type
【问题讨论】:
-
看
std::adjacent_find。 -
这是一个奇怪的错误信息。我原以为有两个参数谓词会使这段代码出错。
-
查找strict weak ordering - 排序时,这是您必须实现的。
return ==不与return <相同。