is_partitioned(vect.begin(), vect.end(), [](int x)
{ return x%2==0;})? cout << "Vector is partitioned": cout << "Vector is not partitioned";
cout << e
ndl;
I'm unable to understand this code due to [](int x)
. please help
is_partitioned(vect.begin(), vect.end(), [](int x)
{ return x%2==0;})? cout << "Vector is partitioned": cout << "Vector is not partitioned";
cout << e
ndl;
I'm unable to understand this code due to [](int x)
. please help
The [](int x)
is actually just a part of the unnamed 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
[]
denotes a capture list
(int x)
is a parameter list
{...}
part is the body
Where the later two are just like in regular functions.
You supply this function (function object) as a predicate to the algorithm is_partitioned
, so to have a custom predicate. Note, that the is_partitioned
, in this case, has the following form:
is_partitioned(first, last, predicate); // Where the predicate is the lambda
For more information, refer to the documentation.