Alexander Bily
Alexander Bily

Reputation: 975

Standard algorithms any_of(), all_of() and none_of() applied on empty ranges

I have a question related to stl algorithms.

From http://www.cplusplus.com/reference/algorithm/ I see that any_of(), all_of() and none_of() have different return values when applied on empty range, but it seems that it is only consequence of their implementations.

What do you consider to be correct return values of these algorithms? Does set theory answer these questions?

Upvotes: 3

Views: 2400

Answers (2)

Karoly Horvath
Karoly Horvath

Reputation: 96266

You simply interpret their name. Use common sense. That is:

any_of does any element in range fulfill the condition? For this, you need at least one matching element.

all_of does all elements in range fulfill the condition? If no elements are there, then all of them meet the criteria.

etc.

The pattern is quite simple:

Upvotes: 8

Dario Griffo
Dario Griffo

Reputation: 4274

This is not about implementation, the standard is pretty clear on what they should return

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf

you can check on page 846 for any_of for example

Returns: false if [first,last) is empty or if there is no iterator i in the range [first,last) such that pred(*i) is true, and true otherwise.

Upvotes: 1

Related Questions