使用 std::all_of 和 boost::irange 来获取循环索引

Use std::all_of with boost::irange to get the loop index

我想知道是否可以同时写一个std::all_of语句和boost::irange来获取我每时每刻正在循环的项目的索引。我的目的是重构一个这样的表达式,看起来它们两个结合起来了:

for (auto instance : boost::irange(instances))
{
    if (not func(instance))
    {
        return false;
    }
}
return true;

函数 func 需要一个整数 instance,我无法重构该函数参数。我想重写为“某物”,例如:

std::all_of(..., ..., [] () { return func(instance); })

可能吗?非常感谢您的建议!

如果您已经在使用 boost,您可以使用 boost::all_of,它有一个带范围和谓词的重载,而不是两个迭代器和一个谓词。

return boost::algorithm::all_of(boost::irange(instances), func);