我正在尝试实现一个在二维 vector 中查找 vector 的函数。我写的是:
vector<vector<int> > result;
vector<int> line;
bool isPresent = find(result.begin(), result.end(), line)
然而,这给出了一个错误
“第 11 行:初始化时无法将‘__gnu_cxx::__normal_iterator*, std::vector >>’转换为‘bool’”
我搜索了很多论坛,但找不到正确的答案。最好的方法是什么?
谢谢, 肖恩
最佳答案
InputIterator std::find(InputIterator first, InputIterator last, const T& val)
Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.
改为使用:
bool isPresent = std::find(result.begin(), result.end(), line) != result.end();
// ^^^^^^^^^^^^^^^
或:
bool isPresent = std::any_of(result.begin(), result.end(),
[&line](const std::vector<int>& x)
{ return x == line; });
关于c++ - 如何在 C++ 中的二维 vector 中查找 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25691398/