【发布时间】:2017-12-20 14:58:33
【问题描述】:
下面有一个用 C++ 编写的代码 sn-p,它不能编译。 原因是试图使用 not1() 反转 lambda 函数的结果。如果有人可以修复此代码,我将不胜感激
#include <iostream> // std::cout
using namespace std;
#include <functional> // std::not1
#include <algorithm> // std::count_if
#include <vector>
int main () {
vector<int> sv = {3, 5, 10,12 };
vector<int> v = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
auto validSelection = [&](auto& e) {
auto isSelected = [&] (auto& sve) {
return e == sve;
};
return find_if(sv.begin(), sv.end(), isSelected) != sv.end();
};
stable_partition(v.begin(), next(v.begin(),8) , not1(validSelection));
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
【问题讨论】:
-
我很确定“不编译”不是错误消息。
-
为什么不使用 lambda 来执行
notl,它更清楚(至少对我而言)。 -
@appleapple 特别是当这段代码已经在不需要的地方使用 lambda 时
-
最小变化:
return find_if(sv.begin(), sv.end(), isSelected) == sv.end(); -
@manni66 只需复制并粘贴 sn-p 并使用您的编译器进行编译。你会看到很多超出我理解的窗口错误信息。如果您能给我一些关于如何解释错误消息的提示,我将不胜感激。
标签: c++ algorithm c++11 lambda