Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything like "std::and" or "std::or"?

Given a container of boolean values (An example is std::vector<bool>), is there a standard function that returns true if all the values are true ("and") or true if at least one value is true ("or"), with short circuit evalutation ?

I digged trough www.cplusplus.com this morning but couldn't find anything close.

like image 298
ereOn Avatar asked Jun 28 '11 12:06

ereOn


People also ask

What does STI stand for sexually?

Sexually transmitted infections (STIs)

Is STI and STD the same thing?

STD stands for “sexually transmitted disease,” and STI stands for “sexually transmitted infection.” But no matter which term people use, they're talking about the same thing: infections that get passed from one person to another during sex.


2 Answers

is there a standard function that returns true if all the values are true ("and")

std::all_of(vec.begin(), vec.end(), [](bool x) { return x; } ) 

or true if at least one value is true ("or")

std::any_of(vec.begin(), vec.end(), [](bool x) { return x; } ) 

with short circuit evalutation?

I just inserted print statements into the lambda, and yes, both functions perform short-circuiting.

like image 99
fredoverflow Avatar answered Sep 28 '22 03:09

fredoverflow


You can implement by:

AND:

std::find(vector.begin(), vector.end(), false) == vector.end() // all the values are true 

OR:

std::find(vector.begin(), vector.end(), true) != vector.end() //at least one value is true 
like image 40
Igor Avatar answered Sep 28 '22 05:09

Igor