How to Use binary_search from STL in C++: Syntax and Example
Use
std::binary_search from the <algorithm> header to check if an element exists in a sorted container. It returns true if the element is found, otherwise false. Make sure the container is sorted before calling binary_search.Syntax
The std::binary_search function has two main forms:
bool binary_search(RandomIt first, RandomIt last, const T& value);bool binary_search(RandomIt first, RandomIt last, const T& value, Compare comp);
Here:
firstandlastare iterators marking the range to search.valueis the element to find.compis an optional comparison function for custom sorting.
The container must be sorted according to the same comparison before calling binary_search.
cpp
template<class RandomIt, class T> bool binary_search(RandomIt first, RandomIt last, const T& value); template<class RandomIt, class T, class Compare> bool binary_search(RandomIt first, RandomIt last, const T& value, Compare comp);
Example
This example shows how to use std::binary_search to check if a number exists in a sorted vector.
cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 3, 5, 7, 9}; // The vector must be sorted for binary_search to work correctly std::sort(numbers.begin(), numbers.end()); int target = 5; if (std::binary_search(numbers.begin(), numbers.end(), target)) { std::cout << target << " found in the vector.\n"; } else { std::cout << target << " not found in the vector.\n"; } target = 6; if (std::binary_search(numbers.begin(), numbers.end(), target)) { std::cout << target << " found in the vector.\n"; } else { std::cout << target << " not found in the vector.\n"; } return 0; }
Output
5 found in the vector.
6 not found in the vector.
Common Pitfalls
Common mistakes when using std::binary_search include:
- Not sorting the container before searching.
binary_searchrequires a sorted range. - Using
binary_searchon unsorted data leads to incorrect results. - Using a different comparison function for sorting and searching causes wrong behavior.
Always ensure the data is sorted with the same criteria before calling binary_search.
cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {3, 1, 7, 5, 9}; // Not sorted int target = 5; // Wrong: binary_search on unsorted vector bool found_wrong = std::binary_search(numbers.begin(), numbers.end(), target); // Correct: sort first std::sort(numbers.begin(), numbers.end()); bool found_right = std::binary_search(numbers.begin(), numbers.end(), target); std::cout << "Wrong result (unsorted): " << (found_wrong ? "found" : "not found") << "\n"; std::cout << "Correct result (sorted): " << (found_right ? "found" : "not found") << "\n"; return 0; }
Output
Wrong result (unsorted): not found
Correct result (sorted): found
Quick Reference
Tips for using std::binary_search:
- Always include
<algorithm>header. - Ensure the container is sorted before searching.
- Use iterators to specify the search range.
- Returns
trueif element is found,falseotherwise. - For custom sorting, provide the same comparison function to
binary_search.
Key Takeaways
std::binary_search checks if an element exists in a sorted range and returns true or false.
Always sort your container before using binary_search to get correct results.
Use the same comparison function for sorting and searching if you customize the order.
binary_search works with iterators, so it can be used with many container types.
Include and use std::binary_search with care to avoid logic errors.