一个简单的算法模板库:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef MY_ALGORITHM_H
#define MY_ALGORITHM_H

#include "my_iterator.h"
#include "my_utility.h"

namespace mystl{

// =========== find ===============
template <typename InputIt, typename T>
InputIt find(InputIt first, InputIt last, const T& value){
for(; first != last; ++first){
if(*first == value){
return first;
}
}
return last;
}

// ============ count =============
template <typename InputIt, typename T>
typename mystl::iterator_traits<InputIt>::difference_type
count(InputIt first, InputIt last, const T& value){
typename mystl::iterator_traits<InputIt>::difference_type n = 0;
for(; first != last; ++first){
if(*first == value){
++n;
}
}
return n;
}


// ============= copy ==============
// template <typename InputIt, typename OutputIt>
// OutputIt copy(InputIt first, InputIt last, OutputIt result){
// for(; first != last; ++first, ++last){
// *result = *first;
// }
// return result;
// }
template <typename InputIt, typename OutputIt>
OutputIt copy(InputIt first, InputIt last, OutputIt result){
for(; first != last; ++first, ++result){
*result = *first;
}
return result;
}



// ============== fill ==============
template <typename ForwardIt, typename T>
void fill(ForwardIt first, ForwardIt last, const T& value){
for(; first != last; ++first){
*first = value;
}
}

// ============== swap ==============
template <typename T>
void swap(T& a, T& b){
T tmp = mystl::move(a);
a = mystl::move(b);
b = mystl::move(tmp);
}


// =============== reverve ===========
template <typename BidirectionalIt>
void reverse(BidirectionalIt first, BidirectionalIt last){
while ((first != last) && (first != --last)){
mystl::swap(*first, *last);
++first;
}
}

// =============== min/max ==============
template <typename T, typename Compare>
const T& min(const T& a, const T& b, Compare comp){
return comp(b, a) ? b : a;
}

template <typename T, typename Compare>
const T& max(const T& a, const T& b, Compare comp){
return comp(a, b) ? b : a;
}

// =============== equal =================
template <typename InputIt1, typename InputIt2>
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2){
for(; first1 != last1; ++first1, ++first2){
if(*first1 != *first2){
return false;
}
}
return true;
}

// =============== lexicographical_compare =================
template <typename InputIt1, typename InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2){
for(; first1 != last1 && first2 != last2; ++first1, ++first2){
if(*first1 < *first2) return true;
if(*first2 < *first1) return false;
}
return (first1 == last1) && (first2 != last2);
}

// =============== for each =============================
template <typename InputIt, typename UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f){
for(; first != last; ++first) f(*first);
return f;
}

// =============== copy backward ===========================
template <typename BidirectionalIt1, typename BidirectionalIt2>
BidirectionalIt2 copy_backward(BidirectionalIt1 first, BidirectionalIt1 last, BidirectionalIt2 d_last){
while (first != last) {
*--d_last = *--last;
}
return d_last;
}


// ============== sort (quick sort impl) ===============
template <typename RandomIt>
void sort(RandomIt first, RandomIt last){
if( first == last) return;
RandomIt i = first;
RandomIt j = last;

--j;

auto pivot = *first;

RandomIt left = first;
RandomIt right = j;

while(left != right){
while(*right >= pivot && left != right) --right;
while(*left <= pivot && left != right) ++left;
if(left != right) mystl::swap(*left, *right);
}

mystl::swap(*first, *left);
sort(first, left);
sort(++left, last);
}

} //namespace mystl

#endif // MY_ALGORITHM_H