-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl_algo.h
More file actions
72 lines (63 loc) · 2.43 KB
/
Copy pathstl_algo.h
File metadata and controls
72 lines (63 loc) · 2.43 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Created by sakura on 2020/5/18.
//
#ifndef SAKURA_STL_STL_ALGO_H
#define SAKURA_STL_STL_ALGO_H
#include "stl_config.h"
__STL_BEGIN_NAMESPACE
//返回指向范围 [first, last) 中首个不小于(即大于或等于) value 的元素的迭代器,或若找不到这种元素则返回 last 。
// Binary search (lower_bound, upper_bound, equal_range, binary_search).
template<class _ForwardIter, class _Tp, class _Distance>
_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
const _Tp &__val, _Distance *) {
_Distance __len = 0;
distance(__first, __last, __len);
_Distance __half;
_ForwardIter __middle;
while (__len > 0) {
__half = __len >> 1;
__middle = __first;
advance(__middle, __half);
if (*__middle < __val) {
__first = __middle;
++__first;
__len = __len - __half - 1;
} else
__len = __half;
}
return __first;
}
template<class _ForwardIter, class _Tp>
inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
const _Tp &__val) {
return __lower_bound(__first, __last, __val,
__DISTANCE_TYPE(__first));
}
template<class _ForwardIter, class _Tp, class _Compare, class _Distance>
_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
const _Tp &__val, _Compare __comp, _Distance *) {
_Distance __len = 0;
distance(__first, __last, __len);
_Distance __half;
_ForwardIter __middle;
while (__len > 0) {
__half = __len >> 1;
__middle = __first;
advance(__middle, __half);
if (__comp(*__middle, __val)) {
__first = __middle;
++__first;
__len = __len - __half - 1;
} else
__len = __half;
}
return __first;
}
template<class _ForwardIter, class _Tp, class _Compare>
inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
const _Tp &__val, _Compare __comp) {
return __lower_bound(__first, __last, __val, __comp,
__DISTANCE_TYPE(__first));
}
__STL_END_NAMESPACE
#endif //SAKURA_STL_STL_ALGO_H