Binary search an element with algorithms!
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers{1, 54, 7, 5335, 8};
std::cout << std::binary_search(numbers.begin(), numbers.end(), 7) << '\n';
}Show answer and explanation
std::binary_search requires the range to be partitioned according to the same ordering used by the search, which a normally sorted range satisfies. Sort first or maintain the invariant; calling it on the unsorted sample violates its precondition, so the result cannot be trusted.
std::binary_search requires the range to be partitioned according to the same ordering used by the search, which a normally sorted range satisfies. Sort first or maintain the invariant; calling it on the unsorted sample violates its precondition, so the result cannot be trusted.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.