c++ - 如何在 int 类型的 vector 中使用 lambda?

标签 c++ lambda

<分区>

我有一个 vector :

std::vector<int> graphPred(tot_lines);

我设置了各种值,但是当输入完成后,我想消除我尝试过的空单元格:

graphPred.erase(std::remove_if(graphPred.begin(), graphPred.end(),
                [](const int graphPred){return graphPred.empty();}),
            graphPred.end());

但是编译器提示“表达式必须是类类型” 我的语法或实现目标的其他方法有问题吗?

最佳答案

“是的,它们是 0”

所以使用这个:

  graphPred.erase(std::remove_if(graphPred.begin(), graphPred.end(),
                    [](const int x){return x==0;}),
                graphPred.end());

你的 lambda 函数

[](const int graphPred){return graphPred.empty();}

int 上使用 empty,这是错误的,成员函数是在类上调用的,这就是错误所在

关于c++ - 如何在 int 类型的 vector 中使用 lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18685633/

相关文章:

C++ 将列表作为参数传递给函数

c++ - 你能用 lambda 比较器交换 std::queue 吗?

c++ - 在 lambda 中移动捕获

c++ - 为什么用项目编译 gtest 而不是使用 lib

c++ - 为什么这会在控制台中返回一个地址?

python 减少: find total size of all lists inside list of tuples

Python lambda if 语句 re.sub

C++:你能做一个 lambda 隐式复制捕获加上显式复制捕获吗?

c++ - 使用 C++ 创建按钮

c++ - 测量函数调用期间的最大内存使用量