Why does std::binary_search return bool?

Stephen DeSalvo

According to draft N4431, the function std::binary_search in the algorithms library returns a bool, [binary.search]:

  template<class ForwardIterator, class T>
  bool binary_search(ForwardIterator first, ForwardIterator last,
                     const T& value);

  template<class ForwardIterator, class T, class Compare>
  bool binary_search(ForwardIterator first, ForwardIterator last,
                     const T& value, Compare comp);

Requires: The elements e of [first,last) are partitioned with respect to the expressions e < value and !(value < e) or comp(e, value) and !comp(value, e). Also, for all elements e of [first,last), e < value implies !(value < e) or comp(e, value) implies !comp(value, e).

Returns: true if there is an iterator i in the range [first,last) that satisfies the corresponding conditions: !(*i < value) && !(value < *i) or comp(*i, value) == false && comp(value, *i) == false.

Complexity: At most log2(last - first) + O(1) comparisons.

Does anyone know why this is the case?

Most other generic algorithms either return an iterator to the element or an iterator that is equivalent to the iterator denoting the end of the sequence of elements (i.e., one after the last element to be considered in the sequence), which is what I would have expected.

Cubbi

The name of this function in 1994 version of STL was isMember. I think you'd agree that a function with that name should return bool

http://www.stepanovpapers.com/Stepanov-The_Standard_Template_Library-1994.pdf

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does std::binary_search use ForwardIterator, not RandomIterator?

From Dev

Why does std::binary_search use ForwardIterator, not RandomIterator?

From Dev

Why does slice::binary_search return an incorrect result?

From Dev

Why is this implemented binary search so much slower than std::binary_search()?

From Java

Why does bool and not bool both return true in this case?

From Dev

Why does std::max return by const&?

From Dev

Why does std::max return the wrong value?

From Dev

Why does using "==" return a Series instead of bool in pandas?

From Dev

why does this binary search succeed a return value in the middle is ignored?

From Dev

why does this binary search succeed a return value in the middle is ignored?

From Dev

How to sort an array of structs with std::binary_search or std::sort

From Dev

Why does std::cbegin return the same type as std::begin

From Dev

How to set needle type in std::binary_search

From Dev

How to set needle type in std::binary_search

From Dev

Why is std::vector<bool> faster?

From Dev

UIAccessibilityCustomAction selector - why return BOOL

From Dev

Why does `return {};` not apply to `std::forward_list`?

From Dev

Why does std::is_assignable return false with related pointer types?

From Java

Why does `std::string::find()` not return the end iterator on failures?

From Dev

Why does std::make_pair return a pair of reference types

From Dev

Why does `return {};` not apply to `std::forward_list`?

From Dev

Why does `search` return -1 and not undefined or null

From Dev

Search in string list and return as bool

From Dev

why std::string is not implicitly converted to bool

From Dev

Why return the Windows BOOL data type and not int?

From Dev

Why does this return fail?

From Dev

Why does hIsEOF not return?

From Dev

Why does GetThreadTimes return

From Dev

Why does operator = return *this?

Related Related

  1. 1

    Why does std::binary_search use ForwardIterator, not RandomIterator?

  2. 2

    Why does std::binary_search use ForwardIterator, not RandomIterator?

  3. 3

    Why does slice::binary_search return an incorrect result?

  4. 4

    Why is this implemented binary search so much slower than std::binary_search()?

  5. 5

    Why does bool and not bool both return true in this case?

  6. 6

    Why does std::max return by const&?

  7. 7

    Why does std::max return the wrong value?

  8. 8

    Why does using "==" return a Series instead of bool in pandas?

  9. 9

    why does this binary search succeed a return value in the middle is ignored?

  10. 10

    why does this binary search succeed a return value in the middle is ignored?

  11. 11

    How to sort an array of structs with std::binary_search or std::sort

  12. 12

    Why does std::cbegin return the same type as std::begin

  13. 13

    How to set needle type in std::binary_search

  14. 14

    How to set needle type in std::binary_search

  15. 15

    Why is std::vector<bool> faster?

  16. 16

    UIAccessibilityCustomAction selector - why return BOOL

  17. 17

    Why does `return {};` not apply to `std::forward_list`?

  18. 18

    Why does std::is_assignable return false with related pointer types?

  19. 19

    Why does `std::string::find()` not return the end iterator on failures?

  20. 20

    Why does std::make_pair return a pair of reference types

  21. 21

    Why does `return {};` not apply to `std::forward_list`?

  22. 22

    Why does `search` return -1 and not undefined or null

  23. 23

    Search in string list and return as bool

  24. 24

    why std::string is not implicitly converted to bool

  25. 25

    Why return the Windows BOOL data type and not int?

  26. 26

    Why does this return fail?

  27. 27

    Why does hIsEOF not return?

  28. 28

    Why does GetThreadTimes return

  29. 29

    Why does operator = return *this?

HotTag

Archive