-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlower_upper_bound.cpp
More file actions
49 lines (31 loc) · 1005 Bytes
/
lower_upper_bound.cpp
File metadata and controls
49 lines (31 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <algorithm>
#include <iostream>
#include <vector>
template <class FwdItr, class T>
constexpr FwdItr lower(FwdItr first, FwdItr end, T const& value) {
FwdItr itr;
typename std::iterator_traits<FwdItr>::difference_type count, step;
count = std::distance(first, end);
while (count > 0) {
itr = first;
step = count/2;
std::cout << "step:" << step << std::endl;
std::advance(itr, step);
if (*itr < value) {
first = ++itr;
std::cout << *first << std::endl;
std::cout << "count:" << count << std::endl;
count -= step + 1;
std::cout << "count:" << count << std::endl;
} else {
count = step;
}
}
return itr;
}
int main() {
std::vector v {0,1,2,3,4,5,6,7,8};
std::cout << *lower(v.begin(), v.end(), 4) << std::endl;
std::cout << *std::lower_bound(v.begin(), v.end(), 4) << std::endl;
return 0;
}