【问题标题】:I'm new in stl c++, someone can explain [](int x)? [duplicate]我是stl c ++的新手,有人可以解释[](int x)吗? [复制]
【发布时间】:2021-06-28 03:29:19
【问题描述】:
is_partitioned(vect.begin(), vect.end(), [](int x)
    { return x%2==0;})? cout << "Vector is partitioned": cout << "Vector is not partitioned";
    cout << e

ndl;

由于[](int x),我无法理解此代码。请帮忙

【问题讨论】:

标签: c++ stl


【解决方案1】:

[](int x)实际上只是未命名lambda function object的一部分:

// Return true if x is divisible by 2, false otherwise
[] (int x) { return x%2 == 0; } // lambda that acts as a predicate
  • [] 表示捕获列表

  • (int x) 是一个参数列表

  • {...} 部分是正文

后两者就像在常规函数中一样。

你提供这个函数(函数对象)作为算法is_partitioned的谓词,所以有一个自定义的谓词。请注意,在这种情况下,is_partitioned 具有以下形式:

is_partitioned(first, last, predicate); // Where the predicate is the lambda

更多信息,请参阅documentation

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 2020-06-03
    • 2017-10-03
    • 2022-01-22
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多