我是 stl c++ 的新手,有人可以解释一下 [](int x) 吗?

0 c++ stl

is_partitioned(vect.begin(), vect.end(), [](int x)
    { return x%2==0;})? cout << "Vector is partitioned": cout << "Vector is not partitioned";
    cout << e
Run Code Online (Sandbox Code Playgroud)

ndl;

由于[](int x). 请帮忙

raw*_*rex 6

[](int x)实际上只是一个无名的一部分lambda函数对象

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

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

  • {...}部分是身体

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

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

is_partitioned(first, last, predicate); // Where the predicate is the lambda
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档