Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::binary_search return bool?

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.

like image 251
Stephen DeSalvo Avatar asked May 28 '15 01:05

Stephen DeSalvo


People also ask

What does Binary_search return in C++?

1. binary_search(start_ptr, end_ptr, num) : This function returns boolean true if the element is present in the container, else returns false.

Does C++ have built in binary search?

C++ bsearch()The bsearch() function in C++ performs a binary search of an element in an array of elements and returns a pointer to the element if found. The bsearch() function requires all elements less than the element to be searched to the left of it in the array.

Does STD find use binary search?

The algorithm to use is std::binary_search , that directly returns a bool representing whether the searched value has equivalent elements in the collection.

Why does std::binary_search return a bool?

I think the rationale behind std::binary_search returning bool is that it wouldn't be clear what iterator to return in case of equivalent elements, while in case of std::lower_bound and std::upper_bound it is clear.

What is the return type of binary_search in the Algorithms Library?

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

What are binary search functions in C++ STL?

Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound) Binary search is an important component in competitive programming or any algorithmic competition, having knowledge of shorthand functions reduces the time to code them. This searching only works when container is sorted.

How do you compare elements in a binary search?

For std::binary_search to succeed, the range [first, last) must be at least partially ordered with respect to value, i.e. it must satisfy all of the following requirements: A fully-sorted range meets these criteria. The first version uses operator< to compare the elements, the second version uses the given comparison function comp .


1 Answers

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

like image 184
Cubbi Avatar answered Oct 04 '22 14:10

Cubbi