In this first part of the big STL algorithm tutorial, I’ll start with the first chunk of the non-modifying sequence operations.

Namely, in this post, you are going to read about all_of, any_of and none_of functions.

Their names are quite intuitive and as you might suspect it, they all return booleans and they operate on STL containers.

Unless you use ranges (that should be part of another post), you don’t pass them directly a container, but rather two iterators on the same container. Those iterators define the range the function will work on.

After the two iterators, you pass in a predicate. That predicate can be a function pointer or a function object (including lambdas) returning a boolean or at least something that is convertible to boolean.

It means that the next code does NOT even compile:

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{

auto nums = {1,2,3,4,5,3};
if (std::any_of(std::begin(nums), std::end(nums), 3) {
std::cout << "there is a 3 in the list" << std::endl;
} else {
std::cout << "there is NOT ANY 3 in the list" << std::endl;
}

}



Instead, let’s see two implementation that works. The first one will use a function object:

#include <iostream>
#include <vector>
#include <algorithm>

class IsEqualTo {
public:
IsEqualTo(int num) : m_num(num) {}

bool operator()(int i) {
return i == m_num;
}
private:
int m_num;
};

int main()
{

auto nums = {1,2,3,4,5,3};
if (std::any_of(std::begin(nums), std::end(nums), IsEqualTo(3))) {
std::cout << "there is a 3 in the list" << std::endl;
} else {
std::cout << "there is NOT ANY 3 in the list" << std::endl;
}

}


It’s a bit long, but thanks to the well-named functor (function object) it is easily readable.

Now let’s have a look at bersion with a lambda expression:

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{

auto nums = {1,2,3,4,5,3};
if (std::any_of(std::begin(nums), std::end(nums), [](int i){return i == 3;})) {
std::cout << "there is a 3 in the list" << std::endl;
} else {
std::cout << "there is NOT ANY 3 in the list" << std::endl;
}

}



This version is a lot shorter, a lot more dense and instead of the whole definition of our class IsEqualTo you have only this lambda expression: [](int i){return i == 3;}).

Now, let’s talk a little bit about what the 3 mentioned functions do, but maybe it’s already clear to you.

### std::all_of

std::all_of will return true if the predicate is evaluated to true or can be converted to true for all the items, false otherwise.

That “can be converted” part means that the predicate doesn’t have to return a boolean. It can return a number for example. But really anything that can be treated as a boolean.

### std::any_of

std::any_of will return true if the predicate is evaluated to true or can be converted to true for any of the items, false otherwise. Meaning that if the predicate is true only for one element out of a hundred, std::any_of will return true.

### std::none_of

std::none_of will return true if the predicate is evaluated to true or can be converted to true for none of the items, false otherwise. Turning it around, std::none_of return true if the predicate is false for all the items! If there is at least one returning true, the function itself will return false.

## Conclusion

That’s it for the first part. The three presented functions - all_of, any_of and none_of - can replace ugly loops with an if and a break inside your code, making it much more expressive and readable. Use them without moderation wherever you can and stay tuned for the next episodes!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8065722,"math_prob":0.94217294,"size":3691,"snap":"2020-24-2020-29","text_gpt3_token_len":932,"char_repetition_ratio":0.12313534,"word_repetition_ratio":0.24242425,"special_character_ratio":0.27174208,"punctuation_ratio":0.18283582,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9680926,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-29T23:46:17Z\",\"WARC-Record-ID\":\"<urn:uuid:25045e75-8503-4e07-9a77-dbce61d097d6>\",\"Content-Length\":\"19069\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1850dd8c-4dba-4953-8104-067dcd1a86a5>\",\"WARC-Concurrent-To\":\"<urn:uuid:a7dc9066-7d6e-48b9-a12e-5c7252597ea6>\",\"WARC-IP-Address\":\"192.30.252.153\",\"WARC-Target-URI\":\"http://sandordargo.com/blog/2019/02/20/stl-algorithm-tutorial-part-1-any-all-none\",\"WARC-Payload-Digest\":\"sha1:7DGF6ZV35NW6NJEVEYYEWFU2XW3V5SEW\",\"WARC-Block-Digest\":\"sha1:5CE5EP4KOPKKQ2MMNNKNSYL2HAQNBDEO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347406785.66_warc_CC-MAIN-20200529214634-20200530004634-00075.warc.gz\"}"}