【发布时间】:2021-01-16 22:05:27
【问题描述】:
如果我运行以下代码,我会得到错误 prog.cpp:7:39: error: no match for 'operator==' (operand types are 'bool' and 'std::vector::iterator {aka __gnu_cxx::__normal_iterator
但如果我使用 find() 而不是 binary_search() 我会得到预期的结果。 这两个函数都只返回一个迭代器,但是为什么它们在这种情况下表现不同呢?
#include <bits/stdc++.h>
using namespace std;
int main ()
{
vector < int >v = { 1, 2, 3, 5 };
if (binary_search (v.begin (), v.end (), 3) == v.end ())
cout << "not found";
else
cout << "found";
}
【问题讨论】:
-
请检查
std::binary_search真正返回的内容。
标签: c++ stl binary-search stdvector