Search⌘ K

Binary Search

Explore how binary search algorithms work in C++ for efficient element searches in sorted containers. Understand functions like std::binary_search, std::lower_bound, std::upper_bound, and std::equal_range. Learn about the importance of sorting criteria and strict weak ordering for reliable search results while optimizing performance with these algorithms.

We'll cover the following...

The binary search algorithms use the fact that the ranges are already sorted. To search for an element, use std::binary_search. With std::lower_bound, we get an iterator for the first element, not smaller than the given value. With std::upper_bound, we get an iterator back for the first element which is bigger than the given value. std:::equal_range combines both ...