std::min_element/std::max_element/std::minmax_element
Use case
Find smallest/largest element in range.
Explanation
Returns iterator, not value. minmax_element is more efficient than calling min_element and max_element separately.
Time complexity: O(n). See possible implementation: std::min_element, std::max_element, std::minmax_element.
Code
#include <algorithm>
#include <cstdint>
#include <print>
#include <vector>
int main() {
std::vector<uint64_t> addresses = {0x10000000, 0x10001000, 0x10002000,
0x10003000};
auto [minIt, maxIt] = std::minmax_element(addresses.begin(), addresses.end());
std::println("Address range: {:#x}-{:#x}", *minIt, *maxIt);
return 0;
}
Output
$ ./src/algorithms/build/std-minmax
Address range: 0x10000000-0x10003000