【问题标题】:How to negate a lambda function result如何否定 lambda 函数结果
【发布时间】: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


【解决方案1】:

其中一种方法是使用包装器std::function。例如

auto even = [](int x) { return x % 2 == 0; };

std::cout << std::not1(std::function<bool(int)>(even))(11) << std::endl;

函数对象适配器 not1 要求用作参数的相应谓词具有 typedef 名称 argument_typeresult_type 并且包装器 std::function 提供它们。

在您的情况下,等效调用如下所示:

stable_partition(v.begin(), next(v.begin(), 8) , not1(function<bool(int&)>(validSelection)));

Demo.

或者,如果我已经正确理解了您要执行的操作,则相应的代码可以如下面的演示程序所示

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

int main()
{
    std::vector<int> sv = { 3, 5, 10, 12 };
    std::vector<int> v = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };

    auto validSelection = [&](int x)
    {
        return std::binary_search(sv.begin(), sv.end(), x);
    };

    std::stable_partition(v.begin(), v.end(), validSelection);

    for (int x : v) std::cout << x << ' ';
    std::cout << std::endl;

    std::stable_partition(v.begin(), v.end(), std::not1( std::function<bool( int )>( validSelection) ) );

    for (int x : v) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

在这种情况下,输出是

3 5 10 12 1 2 4 6 7 8 9 11 13 14 15
1 2 4 6 7 8 9 11 13 14 15 3 5 10 12

注意使用算法std::binary_search,因为向量sv是排序的。

【讨论】:

  • 你应该解释为什么没有包装它就不能工作 - “一元谓词类型必须定义一个成员类型,argument_type,它可以转换为谓词的参数类型。”
  • 实际上向量没有排序。它是出于演示目的而编写的。
  • @SabetayToros 在这种情况下,您可以将 std::binary search 的调用替换为表达式 std::find( .. ) != sv.end()
  • 你可能想提到 C++17 std::not_fn(),它已经有了很大的改进。
  • std::not1std::not2deprecated in C++17 and removed in C++20std::not_fn() 绝对是现在可以使用的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多