c++ - 如何在 C++ 中的二维 vector 中查找 vector ?

标签 c++ c++11 vector

我正在尝试实现一个在二维 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/

相关文章:

C++11 向后兼容性(空整数常量到指针的转换)

C++ for 循环(c++11 方式和旧版本方式)

c++ - 如何取消 std::async 函数?

c++ - 在 C++ 中的 vector 中的结构中搜索枚举

java - vector 列表中的内存泄漏 Java OutOfMemoryError

python - PyEval_CallObject 和 PyObject_CallObject 返回一个空对象

c++ - 基于数组的列表 - 插入函数

c++ - 为什么 vector 下标超出范围?

c++ - 一个进程是否有可能在 Windows 上捕获另一个进程的未处理异常?

用于获取函数签名的 C++ 正则表达式