C++ Algorithm binary_search()30 Aug 2024 | 4 min read C++ Algorithm binary_search() function is used check whether the element in the range [first, last) is equivalent to val (or a binary predicate) and false otherwise.
SyntaxParameterfirst: A forward iterator pointing to the first element in the range to be searched. last: A forward iterator pointing to the past last element in the range to be searched. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements. val: A value of the upper bound to compare the elements in the range. Return valueIt returns true if an element equivalent to val is found otherwise, it returns false. ComplexityOn average, complexity is logarithmic in the distance between first and last: performs up to log2 (N) + 2 element comparisons Where N = last - first. Data racesThe object in the range [first, last) are accessed. ExceptionsThis function throws an exception if either an element comparison or an operation on iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of binary_search(): Output: 4 found Example 2Let's see another simple example: Output: looking for a 3... found! looking for a 6... not found. Example 3Let's see another simple example to compare the elements using comparison function: Output: Here are the values in the vector: 1 2 3 4 5 6 7 9 10 The value 3 was found. The value 8 was not found. Example 4Let's see another simple example: Output: Sorted vector elements : 1 2 3 4 5 6 7 8 9 Searching for 4 : found! Searching for element greater than 9 : not found. Next TopicC++ Algorithm |
C++ Algorithm replace_copy_if() C++ Algorithm replace_copy_if() function is used to make a copy of the range [first, last) to the range beginning at result, placing those for which pred returns true by new_value. It uses predicate pred instead of operator== to compare the elements. This function examines each...
8 min read
C++ Algorithm replace_if() C++ Algorithm replace_if() function is used to assign new_value to all the elements in the range [first, last) for which pred predicate returns true. This function examines each element in a range and replaces it if it satisfies a specified predicate. Syntax template <class ForwardIterator,...
4 min read
C++ Algorithm Functions is_permutation() C++ Algorithm is_permutation() function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching even if in different order. The first range is from [first1, last1) and the...
1 min read
C++ Algorithm Functions search_n() C++ Algorithm search_n() function searches the container [first,last) for the occurrence of a sequence of count elements, that is every element is searched to check whether it satisfies a given pred. An iterator to first element satifisying the condition is returned, else an...
2 min read
C++ Algorithm Functions copy_n() C++ Algorithm copy_n() function specifies the number of elements to be copied into the new container. The function is used to copy n elements of the container [first,last) into a different container starting from result. Syntax template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size...
1 min read
C++ Algorithm merge() C++ Algorithm merge() function is used to merge two sorted ranges [first1, last1) and [first2, last2) into one sorted range beginning at result. Elements are compared using operator < for the first version or using the given binary comparison function comp for the second...
6 min read
C++ Algorithm Function any_of() C++ Algorithm any_of() function tests the value of 'pred' for every element in the range, if for any element the value of pred is true, then the function returns true else return false. Syntax template <class InputIteratir, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate...
1 min read
Introduction: In the dynamic realm of programming, the ability to harness the full potential of hardware resources is a key aspect. Multithreading, the simultaneous execution of multiple threads, plays a crucial role in achieving parallelism and enhancing performance. C++, being a versatile programming language, offers the...
7 min read
C++ Algorithm lower_bound() C++ Algorithm lower_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in an ordered range [first, last) that is not less than (i.e. greater than or equal to) to the specified value...
4 min read
C++ Algorithm unique() C++ Algorithm unique() function is used to transform a sequence in such a way that each duplicate consecutive element becomes a unique element. The first version uses operator== to compare the elements and the second version uses the given binary predicate pred. Syntax equality (1) template <class...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India