More Everyday Problems
Learn and practice with algorithms to deal with everyday problems.
We'll cover the following...
Testing for certain conditions
There are three very handy algorithms called all_of(), any_of(), and none_of(). They all take a range, a unary predicate (a function that takes one argument and returns true or false), and an optional projection function.
Let’s say we have a list of numbers and a small lambda that determines whether a number is negative or not:
We can check if none of the numbers are negative by using none_of():
Further, we can ask if all elements in the list are negative by using all_of():
Lastly, we can see whether the list contains at least one negative number using any_of():
It’s easy to forget about these small, handy building blocks that reside in the standard library. But once we get into the habit of using them, we will never look back and start writing these by hand ...