我是stlc++的新手,有人可以解释一下[](intx)吗?
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). 请帮忙
回答
该[](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
-
[]表示捕获列表 -
(int x)是一个参数列表 -
{...}部分是身体
后两者就像在常规函数中一样。
您提供此函数(函数对象)作为算法的谓词is_partitioned,以便拥有自定义谓词。请注意,is_partitioned在这种情况下, 具有以下形式:
is_partitioned(first, last, predicate); // Where the predicate is the lambda
有关更多信息,请参阅文档。