计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
Most efficient way of evaluating a bool of all items in a vector of <Classtype*>, then returning true if all are true
我有一个指向我创建的类实例的指针向量,其中包含多个值,称为 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;
});
}
当然,您的所有权标志也是如此。
相关文章:
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 如果一个对象是在本地创建的,并在C++中作为异常抛出,那么本地对象如何在其范围之外有效,即在 catch 块中?
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?
- 如果不有效,如何重复用户输入电子邮件
- 计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
- 如果未设置clock_settime,clock_gettime() 是否有效?
- 如果不允许我分配 rvalues 来引用为什么以下代码片段有效,这在内部如何工作?
- g++:如果在 -O2 或 -O3 之前指定,-fno-omit-frame-pointer 是否有效
- 如果没有'&&'语句有效?
- 如果 null 不是有效值,则使参数成为引用而不是指针
- 如果ConstexPR似乎仅在两种情况有效时起作用
- 有效地测试 QSet 是否包含元素,如果不存在,则插入
- 如果所有输入都有效,则打印总和,否则在 C++ 中打印"Invalid"
- 如果没有重复,则可以有效地将整数插入到不断增长的数组中
- 如果使用alloca在内联函数中的堆栈上分配变量,那么在内联函数返回后,其引用是否有效
- 如果我把一个局部对象移到一个函数中,它以后仍然有效吗
- 如果索引超过数组末尾有效,为什么我需要指定数组长度
- 如何在循环中更改 int 变量,如果是这样,是否有更有效的方法
- 此枚举是否有效,如果有效,为什么