Back In the print window choose "Save as PDF" as the destination
NOTE.CS

lower_bound and upper_bound

1 topic · 28 Nov 2026
1

lower_bound and upper_bound

Algorithm › Search

STL binary search helpers on sorted ranges. lower_bound → first element ≥ x. upper_bound → first element > x.

C++
vector<int> v = {1, 2, 4, 4, 4, 7, 9};
auto lo = lower_bound(v.begin(), v.end(), 4) - v.begin();   // 2
auto hi = upper_bound(v.begin(), v.end(), 4) - v.begin();   // 5
cout << hi - lo;                                            // 3 = count of 4
cout << binary_search(v.begin(), v.end(), 7);               // 1 (true)