std::binary_search() in C++ STL Last Updated : 04 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, STL provide std::binary_search() function which implements binary search algorithm to check whether an element exists in the given sorted range. It is defined inside <algorithm> header file. In this article, we will learn about std::binary_search() function in C++.Example: C++ // C++ Program to illustrate the use of // std::binary_search() method #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 6, 8, 9}; int k = 8; if (binary_search(v.begin(), v.end(), k)) cout << k << " is Present"; else cout << k << " is NOT Present"; return 0; } Output8 is Present binary_search() Syntaxstd::binary_search(first, last, k, comp);Parametersfirst: Iterator to the first element of the range.last: Iterator to the theoretical element just after the last element of range.k: The value to be search.comp: Custom comparator. By default, it returns true if k is equal to the current element in the range.Return ValueReturns true, if k is present in the given range.Returns false, if k is not present in the given range.Note: Behaviour of binary_search() is undefined if the given range is not sorted as binary search algorithm can only be implemented on sorted data.More Examples of binary_search()The following examples demonstrates the different use cases of std::binary_seach() function:Example 1: Checking if an Element Exists in Array using std::binary_search() C++ // C++ Program to check if an element exists in // in an array using std::binary_search() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 4, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); int k = 7; // Check if the element exists if (binary_search(arr, arr + n, k)) cout << k << " is Present"; else cout << k << " is NOT Present"; return 0; } Output78 is Present Example 2: Using binary_search() on a Set Container C++ // C++ Program to check if an element exists in // in an set using std::binary_search() #include <bits/stdc++.h> using namespace std; int main() { set<int> s = {1, 4, 5, 7, 9}; int k = 8; // Check if the element exists if (binary_search(s.begin(), s.end(), k)) cout << k << " is Present"; else cout << k << " is NOT Present"; return 0; } Output11 is Present Explanation: Even though set container does not have random access, binary_search() function still works because of the reason discussed below. Comment More infoAdvertise with us Next Article std::binary_search() in C++ STL kartik Follow Improve Article Tags : C++ Binary Search STL cpp-algorithm-library Practice Tags : CPPBinary SearchSTL Similar Reads std::search in C++ std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence. It searches the sequence [first1, last1) for the first occurrence of the subsequence defi 4 min read Linear Search vs Binary Search Prerequisite: Linear SearchBinary SearchLINEAR SEARCH Assume that item is in an array in random order and we have to find an item. Then the only way to search for a target item is, to begin with, the first position and compare it to the target. If the item is at the same, we will return the position 11 min read In what situation can we use binary search? Binary search is a powerful algorithm that can be used to find a target value within a sorted array. It works by repeatedly dividing the array in half until the target value is found or the array is empty. This makes binary search much faster than linear search, which must check every element in the 3 min read Binary Search in PHP Binary Search is a searching technique used to search an element in a sorted array. In this article, we will learn about how to implement Binary Search in PHP using iterative and recursive way. Given a array of numbers, we need to search for the presence of element x in the array using Binary Search 3 min read How to do binary search step by step? Binary search is an efficient search algorithm that works on sorted arrays or lists. It repeatedly divides the search space in half until the target element is found or the search space is exhausted. Step-by-Step Guide to Perform Binary Search:Step 1: Initialize Variableslow: Set this variable to 0, 3 min read Like