std::bsearch(3)
β¨ NAME β¨
std::bsearch β literally std::bsearch, bestie. This is the std::bsearch(3) page from the C++ Standard Libary manual (yes, that manual name is being folded in right here instead of copied as a header line π).
π¦ Synopsis π¦
okay so first, the header tea and the exact signature vibes:
Defined in header <cstdlib>
void* bsearch( const void* key, const void* ptr, std::size_t
count,
std::size_t size, /* c-compare-pred */* comp
); (1)
void* bsearch( const void* key, const void* ptr, std::size_t
count,
std::size_t size, /* compare-pred */* comp );
extern "C" using /* c-compare-pred */ = int(const void*,
const void*); (2)
(exposition only*)
extern "C++" using /* compare-pred */ = int(const void*,
const void*);
So std::bsearch finds an element equal to the element pointed to by key in an array pointed to by ptr. The array contains count elements of size bytes each and must be partitioned with respect to the object pointed to by key β meaning all the elements that compare less than it have to appear before all the elements that compare equal to it, and those have to appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using the function pointed to by comp.
π¨ Undefined behavior alert: the behavior is undefined if the array is not already partitioned in ascending order with respect tokey, according to the same criterion thatcompuses. Not βmessy,β not βbest effortβ β undefined, bestie. π
If the array contains several elements that comp would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result. So donβt @ me about which twin you get π.
π Parameters π
keyβ pointer to the element to search for.ptrβ pointer to the array to examine.countβ number of element in the array.sizeβ size of each element in the array in bytes.compβ comparison function which returns a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second, and zero if the arguments are equivalent.keyis passed as the first argument, an element from the array as the second.
The signature of the comparison function should be equivalent to the following:
int cmp(const void *a, const void *b);
The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array. Periodt.
π₯ Return value π₯
Pointer to the found element or null pointer if the element has not been found. Thatβs it β no extra lore, no secret third thing π .
πΈ Notes πΈ
Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees. So the name is giving binary search, but the standard is not contractually obligated, iykyk.
The two overloads provided by the C++ standard library are distinct because the types of the parameter comp are distinct (language linkage is part of its type). Iconic type-system behavior, honestly.
π Example π
// Run this code
#include <array>
#include <cstdlib>
#include <iostream>
template<typename T>
int compare(const void *a, const void *b)
{
const auto &arg1 = *(static_cast<const T*>(a));
const auto &arg2 = *(static_cast<const T*>(b));
const auto cmp = arg1 <=> arg2;
return cmp < 0 ? -1
: cmp > 0 ? +1
: 0;
}
int main()
{
std::array arr{1, 2, 3, 4, 5, 6, 7, 8};
for (const int key : {4, 8, 9})
{
const int* p = static_cast<int*>(
std::bsearch(&key,
arr.data(),
arr.size(),
sizeof(decltype(arr)::value_type),
compare<int>));
std::cout << "value " << key;
if (p)
std::cout << " found at position " << (p - arr.data()) <<
'\n'
else
std::cout << " not found\n";
}
}
Output:
value 4 found at position 3
value 8 found at position 7
value 9 not found
π See also π
qsortβ sorts a range of elements with unspecified type (function).equal_rangeβ returns range of elements matching a specific key (function template).- C documentation for
bsearch.
Source/version/date sign-off, bestie: this iconic page came from http://cppreference.com, version/date 2024.06.10, and the page title is std::bsearch(3). Mwah π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms