我在 Lambda 上做错了什么
What am I doing wrong with Lambda
>我有一个问题。首先,考虑这个名为"探索"的 2D 矢量:
std::vector<std::vector<int>>explored;
现在,考虑一下,我有一个函数,我将这个向量和两个整数传递给它,称为 row 和 col:
bool check(std::vector<std::vector<int>> const explored, int row, int col);
现在,我想为这个 2D 向量实现 std::find 并检查它的向量(顺便说一下,它们都有 2 个整数值(是否等于行和列:
explored[n][0] == row && explored[n][1] == col;
所以,我写了这个:
if(std::find(explored.begin(), explored.end(), [row,col](vector<int> a, int row, int col){ return a[0] == row && a[1] == col;}) == explored.end()){
return true;
}
我在这里做错了什么?我的编译器(即Xcode GNU(给出了这个错误:
error: invalid operands to binary expression ('const std::__1::vector<int, std::__1::allocator<int> >' and 'const (lambda at /Users/abylikhsanov/CLionProjects/bfs/main.cpp:8:60)')
if (*__first == __value_)
如果要将谓词传递给查找实用程序,std::find_if
就是您要查找的。
此外,请注意,谓词的签名是:
bool pred(const Type &a);
因此,它将您的示例转换为如下所示的内容:
if(std::find_if(explored.begin(), explored.end(), [row,col](const vector<int> &a){ return a[0] == row && a[1] == col;}) == explored.end()){
return true;
}
我还使用了一个 const 引用作为 lambda 的参数,以避免每次调用函数时来回复制向量。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?