计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true

Most efficient way of evaluating a bool of all items in a vector of <Classtype*>, then returning true if all are true

本文关键字:如果 有效 全部 方法 true 返回 布尔值 向量 gt Classtype 计算      更新时间:2023-10-16

我有一个指向我创建的类实例的指针向量,其中包含多个值,称为 Record它有一个值调用,当我访问它们时,我

bool recordDeleted; bool recordOwnership;

vector<Record*> RecordsVec

我想创建一个函数来执行类似操作的函数,

bool func()
{

    for (auto it = RecordsVec.begin(); it < RecordsVec.end(); it++)
    {
       // check whether recordDeleted is true // or recordOwnership == true)

    }
    // if all are true
       // return true
       // else
       // return false
}
最有效的

方法是什么?

很简单:

bool allDeleted() {
    return std::all_of(begin(RecordsVec), end(RecordsVec), [](Record *r) {
        return r->recordDeleted;
    });
}

当然,您的所有权标志也是如此。

相关文章: