Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a4c0b7f

Browse files
Upload note code of chapter 6 algorithms
1 parent 0f72b19 commit a4c0b7f

31 files changed

+5137
-0
lines changed

‎6_STL_algorithms/6_3_1_numeric.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// file: 6numeric.cpp
2+
3+
#include <numeric>
4+
#include <vector>
5+
#include <functional> // minus<int>()
6+
#include <iostream> // ostream_iterator
7+
#include <iterator>
8+
9+
using namespace std;
10+
11+
int main() {
12+
int ia[5] = {1, 2, 3, 4, 5};
13+
vector<int> iv(ia, ia + 5);
14+
// 0+1+2+3...
15+
cout << accumulate(iv.begin(), iv.end(), 0) << endl;
16+
17+
// 0-1-2-3
18+
cout << accumulate(iv.begin(), iv.end(), 0, minus<int>()) << endl;
19+
20+
// 10 + 1*1 + 2*2 + ...
21+
cout << inner_product(iv.begin(), iv.end(), iv.begin(), 10) << endl;
22+
23+
// 10 - 1+1 - 2+2 - ...
24+
cout << inner_product(iv.begin(), iv.end(), iv.begin(), 10,
25+
minus<int>(), plus<int>()) << endl;
26+
27+
// 将迭代器绑定到cout,作为输出用
28+
ostream_iterator<int> oite(cout, " ");
29+
30+
// 1 3 6 10 15 累计和
31+
partial_sum(iv.begin(), iv.end(), oite);
32+
cout << endl;
33+
34+
// 1 -1 -4 -8 -13 累计差
35+
partial_sum(iv.begin(), iv.end(), oite, minus<int>());
36+
cout << endl;
37+
38+
// 1 1 1 1 1 new #n = #n - #n-1
39+
adjacent_difference(iv.begin(), iv.end(), oite);
40+
cout << endl;
41+
42+
// 1 3 5 7 9 new #n = op(#n, #n-1)
43+
adjacent_difference(iv.begin(), iv.end(), oite, plus<int>());
44+
cout << endl;
45+
46+
// mingw c++ 中stl没有power实现
47+
// cout << power(10, 3) << endl;
48+
// cout << power(10, 3, plus<int>()) << endl;
49+
50+
int n = 3;
51+
iota(iv.begin(), iv.end(), n); // 填入n, n+1, n+2
52+
for (int i = 0; i < iv.size(); ++i)
53+
cout << iv[i] << ' ';
54+
}

‎6_STL_algorithms/6_3_2_stl_numeric.h‎

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
*
3+
* Copyright (c) 1994
4+
* Hewlett-Packard Company
5+
*
6+
* Permission to use, copy, modify, distribute and sell this software
7+
* and its documentation for any purpose is hereby granted without fee,
8+
* provided that the above copyright notice appear in all copies and
9+
* that both that copyright notice and this permission notice appear
10+
* in supporting documentation. Hewlett-Packard Company makes no
11+
* representations about the suitability of this software for any
12+
* purpose. It is provided "as is" without express or implied warranty.
13+
*
14+
*
15+
* Copyright (c) 1996,1997
16+
* Silicon Graphics Computer Systems, Inc.
17+
*
18+
* Permission to use, copy, modify, distribute and sell this software
19+
* and its documentation for any purpose is hereby granted without fee,
20+
* provided that the above copyright notice appear in all copies and
21+
* that both that copyright notice and this permission notice appear
22+
* in supporting documentation. Silicon Graphics makes no
23+
* representations about the suitability of this software for any
24+
* purpose. It is provided "as is" without express or implied warranty.
25+
*/
26+
27+
/* NOTE: This is an internal header file, included by other STL headers.
28+
* You should not attempt to use it directly.
29+
*/
30+
31+
32+
#ifndef __SGI_STL_INTERNAL_NUMERIC_H
33+
#define __SGI_STL_INTERNAL_NUMERIC_H
34+
35+
__STL_BEGIN_NAMESPACE
36+
37+
// 必须提供init值,使得区间为空时仍能返回值
38+
template <class _InputIterator, class _Tp>
39+
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
40+
{
41+
__STL_REQUIRES(_InputIterator, _InputIterator);
42+
for ( ; __first != __last; ++__first)
43+
__init = __init + *__first; // 将每个元素值累加
44+
return __init;
45+
}
46+
47+
template <class _InputIterator, class _Tp, class _BinaryOperation>
48+
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
49+
_BinaryOperation __binary_op)
50+
{
51+
__STL_REQUIRES(_InputIterator, _InputIterator);
52+
for ( ; __first != __last; ++__first)
53+
__init = __binary_op(__init, *__first); // 对每一个元素进行二元操作
54+
return __init;
55+
}
56+
57+
template <class _InputIterator1, class _InputIterator2, class _Tp>
58+
_Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
59+
_InputIterator2 __first2, _Tp __init)
60+
{
61+
__STL_REQUIRES(_InputIterator2, _InputIterator);
62+
__STL_REQUIRES(_InputIterator2, _InputIterator);
63+
// 执行两个序列的内积
64+
for ( ; __first1 != __last1; ++__first1, ++__first2)
65+
__init = __init + (*__first1 * *__first2);
66+
return __init;
67+
}
68+
69+
template <class _InputIterator1, class _InputIterator2, class _Tp,
70+
class _BinaryOperation1, class _BinaryOperation2>
71+
_Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
72+
_InputIterator2 __first2, _Tp __init,
73+
_BinaryOperation1 __binary_op1,
74+
_BinaryOperation2 __binary_op2)
75+
{
76+
__STL_REQUIRES(_InputIterator2, _InputIterator);
77+
__STL_REQUIRES(_InputIterator2, _InputIterator);
78+
// 以提供的仿函数执行两个序列运算
79+
for ( ; __first1 != __last1; ++__first1, ++__first2)
80+
__init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
81+
return __init;
82+
}
83+
84+
template <class _InputIterator, class _OutputIterator, class _Tp>
85+
_OutputIterator
86+
__partial_sum(_InputIterator __first, _InputIterator __last,
87+
_OutputIterator __result, _Tp*)
88+
{
89+
_Tp __value = *__first;
90+
while (++__first != __last) {
91+
__value = __value + *__first; // 前n项和
92+
*++__result = __value; // 赋值
93+
}
94+
return ++__result;
95+
}
96+
97+
template <class _InputIterator, class _OutputIterator>
98+
_OutputIterator
99+
partial_sum(_InputIterator __first, _InputIterator __last,
100+
_OutputIterator __result)
101+
{
102+
__STL_REQUIRES(_InputIterator, _InputIterator);
103+
__STL_REQUIRES(_OutputIterator, _OutputIterator);
104+
if (__first == __last) return __result;
105+
*__result = *__first;
106+
return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first));
107+
}
108+
109+
template <class _InputIterator, class _OutputIterator, class _Tp,
110+
class _BinaryOperation>
111+
_OutputIterator
112+
__partial_sum(_InputIterator __first, _InputIterator __last,
113+
_OutputIterator __result, _Tp*, _BinaryOperation __binary_op)
114+
{
115+
_Tp __value = *__first;
116+
while (++__first != __last) {
117+
__value = __binary_op(__value, *__first); // 前n项运算结果
118+
*++__result = __value; // 赋值
119+
}
120+
return ++__result;
121+
}
122+
123+
template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
124+
_OutputIterator
125+
partial_sum(_InputIterator __first, _InputIterator __last,
126+
_OutputIterator __result, _BinaryOperation __binary_op)
127+
{
128+
__STL_REQUIRES(_InputIterator, _InputIterator);
129+
__STL_REQUIRES(_OutputIterator, _OutputIterator);
130+
if (__first == __last) return __result;
131+
*__result = *__first;
132+
return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first),
133+
__binary_op);
134+
}
135+
136+
template <class _InputIterator, class _OutputIterator, class _Tp>
137+
_OutputIterator
138+
__adjacent_difference(_InputIterator __first, _InputIterator __last,
139+
_OutputIterator __result, _Tp*)
140+
{
141+
_Tp __value = *__first; // 记录第一个元素
142+
while (++__first != __last) {
143+
_Tp __tmp = *__first;
144+
*++__result = __tmp - __value; // 差值赋值
145+
__value = __tmp;
146+
}
147+
return ++__result;
148+
}
149+
150+
template <class _InputIterator, class _OutputIterator>
151+
_OutputIterator
152+
adjacent_difference(_InputIterator __first,
153+
_InputIterator __last, _OutputIterator __result)
154+
{
155+
__STL_REQUIRES(_InputIterator, _InputIterator);
156+
__STL_REQUIRES(_OutputIterator, _OutputIterator);
157+
if (__first == __last) return __result;
158+
*__result = *__first;
159+
// 侯捷原注解:可以直接萃取类型,无需传递调用
160+
// iterator_traits<InputIterator>::value_type value = *first;
161+
// while (++__first != __last) {
162+
// _Tp __tmp = *__first;
163+
// *++__result = __tmp - __value; // 差值赋值
164+
// __value = __tmp;
165+
// }
166+
// return ++__result;
167+
return __adjacent_difference(__first, __last, __result,
168+
__VALUE_TYPE(__first));
169+
}
170+
171+
template <class _InputIterator, class _OutputIterator, class _Tp,
172+
class _BinaryOperation>
173+
_OutputIterator
174+
__adjacent_difference(_InputIterator __first, _InputIterator __last,
175+
_OutputIterator __result, _Tp*,
176+
_BinaryOperation __binary_op) {
177+
_Tp __value = *__first; // 记录第一个元素
178+
while (++__first != __last) {
179+
_Tp __tmp = *__first;
180+
*++__result = __binary_op(__tmp, __value); // 将相邻元素的运算结果赋值
181+
__value = __tmp;
182+
}
183+
return ++__result;
184+
}
185+
186+
template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
187+
_OutputIterator
188+
adjacent_difference(_InputIterator __first, _InputIterator __last,
189+
_OutputIterator __result, _BinaryOperation __binary_op)
190+
{
191+
__STL_REQUIRES(_InputIterator, _InputIterator);
192+
__STL_REQUIRES(_OutputIterator, _OutputIterator);
193+
if (__first == __last) return __result;
194+
*__result = *__first;
195+
return __adjacent_difference(__first, __last, __result,
196+
__VALUE_TYPE(__first),
197+
__binary_op);
198+
}
199+
200+
// Returns __x ** __n, where __n >= 0. _Note that "multiplication"
201+
// is required to be associative, but not necessarily commutative.
202+
203+
// SGI专属 power,不是标准
204+
// _MonoidOperation 必须满足结合律
205+
template <class _Tp, class _Integer, class _MonoidOperation>
206+
_Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr)
207+
{
208+
if (__n == 0)
209+
return identity_element(__opr); // 取出证同元素,见7.3节
210+
else {
211+
while ((__n & 1) == 0) {
212+
__n >>= 1;
213+
__x = __opr(__x, __x);
214+
}
215+
216+
_Tp __result = __x;
217+
__n >>= 1;
218+
while (__n != 0) {
219+
__x = __opr(__x, __x);
220+
if ((__n & 1) != 0)
221+
__result = __opr(__result, __x);
222+
__n >>= 1;
223+
}
224+
return __result;
225+
}
226+
}
227+
228+
template <class _Tp, class _Integer>
229+
inline _Tp __power(_Tp __x, _Integer __n)
230+
{
231+
return __power(__x, __n, multiplies<_Tp>()); // 指定为累乘
232+
}
233+
234+
// Alias for the internal name __power. Note that power is an extension,
235+
// not part of the C++ standard.
236+
237+
template <class _Tp, class _Integer, class _MonoidOperation>
238+
inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr)
239+
{
240+
return __power(__x, __n, __opr);
241+
}
242+
243+
template <class _Tp, class _Integer>
244+
inline _Tp power(_Tp __x, _Integer __n)
245+
{
246+
return __power(__x, __n);
247+
}
248+
249+
// iota is not part of the C++ standard. It is an extension.
250+
// 非标准,SGI专属,设定区间递增内容
251+
template <class _ForwardIter, class _Tp>
252+
void
253+
iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
254+
{
255+
__STL_REQUIRES(_ForwardIter, _Mutable_ForwardIterator);
256+
__STL_CONVERTIBLE(_Tp, typename iterator_traits<_ForwardIter>::value_type);
257+
while (__first != __last)
258+
*__first++ = __value++;
259+
}
260+
261+
__STL_END_NAMESPACE
262+
263+
#endif /* __SGI_STL_INTERNAL_NUMERIC_H */
264+
265+
// Local Variables:
266+
// mode:C++
267+
// End:

‎6_STL_algorithms/6_4_1_algobase.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// file: 6algobase.cpp
2+
3+
#include <algorithm>
4+
#include <vector>
5+
#include <functional>
6+
#include <iostream>
7+
#include <iterator>
8+
#include <string>
9+
10+
using namespace std;
11+
12+
template <class T>
13+
struct display {
14+
void operator() (const T& x) const {
15+
cout << x << ' ';
16+
}
17+
};
18+
19+
20+
int main() {
21+
int ia[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
22+
// int ib = iota(ia, ia + 9, 0); // include numeric
23+
24+
vector<int> iv1(ia, ia + 5);
25+
vector<int> iv2(ia, ia + 9);
26+
27+
// 判断两个序列第一个不匹配点 pair(第一个序列的不匹配点,第二个序列的不匹配点)
28+
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).first);
29+
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).second);
30+
31+
// 如果序列在区间内相等,equal返回true
32+
cout << equal(iv1.begin(), iv1.end(), iv2.begin());
33+
cout << equal(iv1.begin(), iv1.end(), &ia[3]);
34+
cout << equal(iv1.begin(), iv1.end(), &ia[3], less<int>());
35+
36+
fill(iv1.begin(), iv1.end(), 9);
37+
for_each(iv1.begin(), iv1.end(), display<int>());
38+
39+
fill_n(iv1.begin(), 3, 7); // 从迭代器开始填3个7
40+
for_each(iv1.begin(), iv1.end(), display<int>());
41+
42+
vector<int>::iterator ite1 = iv1.begin();
43+
vector<int>::iterator ite2 = ite1; // 指向7
44+
advance(ite2, 3); // 指向9,前进
45+
46+
iter_swap(ite1, ite2);
47+
cout << *ite1 << ' ' << *ite2 << endl;
48+
for_each(iv1.begin(), iv1.end(), display<int>());
49+
50+
cout << max(*ite1, *ite2) << endl;
51+
cout << min(*ite1, *ite2) << endl;
52+
53+
// 错误写法
54+
cout << *max(ite1, ite2) << endl;
55+
cout << *min(ite1, ite2) << endl;
56+
57+
swap(*iv1.begin(), *iv2.begin());
58+
for_each(iv1.begin(), iv1.end(), display<int>());
59+
for_each(iv2.begin(), iv2.end(), display<int>());
60+
61+
string stra1[] = {"Jamie", "JJHou", "Jason"};
62+
string stra2[] = {"Jamie", "JJHou", "Jerry"};
63+
64+
cout << lexicographical_compare(stra1, stra1 + 2, stra2, stra2 + 2);
65+
cout << lexicographical_compare(stra1, stra1 + 2, stra2, stra2 + 2,
66+
greater<string>());
67+
68+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /