Skip to content

Commit 605f27c

Browse files
committed
Upload note code of chapter 8 adpater
1 parent 2eb6d96 commit 605f27c

12 files changed

+2031
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// file: 8iterator-adapter.cpp
2+
3+
#include <iterator> // for iterator adapters
4+
#include <deque>
5+
#include <algorithm> // for copy()
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
int main() {
11+
// 将outite绑定到cout,每次对outite指派一个元素,就后接一个“ ”
12+
ostream_iterator<int> outite(cout, " ");
13+
14+
int ia[] = {0, 1, 2, 3, 4, 5};
15+
deque<int> id(ia, ia + 6);
16+
17+
// 将所有元素拷贝到outite,即cout
18+
copy(id.begin(), id.end(), outite);
19+
cout << endl;
20+
21+
// 将ia[]的部分元素拷贝到id内,使用front_insert_iterator
22+
// front_insert_iterator会将assign操作给push_front操作
23+
// vector不支持push_front操作,所以不以vector做示范对象
24+
copy(ia + 1, ia + 2, front_inserter(id));
25+
copy(id.begin(), id.end(), outite);
26+
cout << endl;
27+
28+
// 将ia[]的部分元素拷贝到id内,使用back_insert_iterator
29+
copy(ia + 1, ia + 2, back_inserter(id));
30+
copy(id.begin(), id.end(), outite);
31+
cout << endl;
32+
33+
// 搜寻元素5所在位置
34+
deque<int>::iterator ite = find(id.begin(), id.end(), 5);
35+
// 将ia[]的部分元素拷贝到id内,使用insert_iterator
36+
copy(ia + 1, ia + 2, inserter(id, ite));
37+
copy(id.begin(), id.end(), outite);
38+
cout << endl;
39+
40+
// 将所有元素逆向拷贝到outite
41+
// rbegin()和rend()与reverse_iterator有关
42+
copy(id.rbegin(), id.rend(), outite);
43+
cout << endl;
44+
45+
// 将inite绑定到cin,将元素拷贝到inite,知道eos出现
46+
istream_iterator<int> inite(cin), eos; // eos: end-of-stream
47+
copy(inite, eos, inserter(id, id.begin()));
48+
// 输入数字,停止时可以输入任意字符
49+
50+
copy(id.begin(), id.end(), outite);
51+
}

8_STL_adapter/8_1_3_compose.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// file: 8compose.cpp
2+
3+
#include <algorithm>
4+
#include <functional>
5+
#include <vector>
6+
#include <iostream>
7+
#include <iterator>
8+
9+
using namespace std;
10+
11+
// compose1在mingw中没有,所以把定义搬了过来
12+
template <class _Operation1, class _Operation2>
13+
class unary_compose
14+
: public unary_function<typename _Operation2::argument_type,
15+
typename _Operation1::result_type>
16+
{
17+
protected:
18+
_Operation1 _M_fn1;
19+
_Operation2 _M_fn2;
20+
public:
21+
unary_compose(const _Operation1& __x, const _Operation2& __y)
22+
: _M_fn1(__x), _M_fn2(__y) {}
23+
typename _Operation1::result_type
24+
operator()(const typename _Operation2::argument_type& __x) const {
25+
return _M_fn1(_M_fn2(__x));
26+
}
27+
};
28+
29+
template <class _Operation1, class _Operation2>
30+
inline unary_compose<_Operation1,_Operation2>
31+
compose1(const _Operation1& __fn1, const _Operation2& __fn2)
32+
{
33+
return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
34+
}
35+
36+
int main() {
37+
// 将outite绑定到cout,每次对outite指派一个元素,就后接一个“ ”
38+
ostream_iterator<int> outite(cout, " ");
39+
40+
int ia[6] = {2, 21, 12, 7, 19, 23};
41+
vector<int> iv(ia, ia + 6);
42+
43+
// 想执行v = (v+2)*3
44+
// for_each是non-mutating algorithm,元素内容不能更改
45+
// 所以执行后iv内容不变
46+
for_each(iv.begin(), iv.end(),
47+
compose1(bind2nd(multiplies<int>(), 3),
48+
bind2nd(plus<int>(), 2)));
49+
copy(iv.begin(), iv.end(), outite);
50+
cout << endl;
51+
52+
transform(iv.begin(), iv.end(), outite,
53+
compose1(bind2nd(multiplies<int>(), 3),
54+
bind2nd(plus<int>(), 2)));
55+
cout << endl;
56+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// file: 8functor-adapter.cpp
2+
3+
#include <algorithm>
4+
#include <functional>
5+
#include <vector>
6+
#include <iostream>
7+
#include <iterator>
8+
9+
using namespace std;
10+
11+
// compose1在mingw中没有,所以把定义搬了过来
12+
template <class _Operation1, class _Operation2>
13+
class unary_compose
14+
: public unary_function<typename _Operation2::argument_type,
15+
typename _Operation1::result_type>
16+
{
17+
protected:
18+
_Operation1 _M_fn1;
19+
_Operation2 _M_fn2;
20+
public:
21+
unary_compose(const _Operation1& __x, const _Operation2& __y)
22+
: _M_fn1(__x), _M_fn2(__y) {}
23+
typename _Operation1::result_type
24+
operator()(const typename _Operation2::argument_type& __x) const {
25+
return _M_fn1(_M_fn2(__x));
26+
}
27+
};
28+
29+
template <class _Operation1, class _Operation2>
30+
inline unary_compose<_Operation1,_Operation2>
31+
compose1(const _Operation1& __fn1, const _Operation2& __fn2)
32+
{
33+
return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
34+
}
35+
36+
37+
void print(int i) {
38+
cout << i << " ";
39+
}
40+
41+
class Int {
42+
public:
43+
// explicit 只能修改只有一个参数的类构造函数,
44+
// 或是除了第一个参数外其他参数都有默认值的情况
45+
// 表明该构造函数是显示的而非隐式的
46+
// 作用是禁止类构造函数的隐式自动转换
47+
// implicit 表示隐式,类构造函数默认声明为隐式
48+
// google c++规范与effective c++都推荐使用explicit声明
49+
explicit Int(int i) : m_i(i) {}
50+
51+
void print1() const {
52+
cout << "[" << m_i << "]";
53+
}
54+
55+
private:
56+
int m_i;
57+
};
58+
59+
int main() {
60+
// 将outite绑定到cout,每次对outite指派一个元素,就后接一个“ ”
61+
ostream_iterator<int> outite(cout, " ");
62+
63+
int ia[6] = {2, 21, 12, 7, 19, 23};
64+
vector<int> iv(ia, ia + 6);
65+
66+
// 找出不小于12的元素个数
67+
cout << count_if(iv.begin(), iv.end(),
68+
not1(bind2nd(less<int>(), 12)));
69+
cout << endl;
70+
71+
// 令每个元素v执行(v+2)*3然后输往outite
72+
transform(iv.begin(), iv.end(), outite,
73+
compose1(bind2nd(multiplies<int>(), 3),
74+
bind2nd(plus<int>(), 2)));
75+
cout << endl;
76+
77+
// 将所有元素拷贝到outite
78+
copy(iv.begin(), iv.end(), outite);
79+
cout << endl;
80+
// 1. 使用函数指针搭配stl算法
81+
for_each(iv.begin(), iv.end(), print);
82+
cout << endl;
83+
84+
// 2. 以修饰过的一般函数搭配stl算法
85+
for_each(iv.begin(), iv.end(), ptr_fun(print));
86+
cout << endl;
87+
88+
Int t1(3), t2(7), t3(20), t4(14), t5(68);
89+
vector<Int> Iv;
90+
Iv.push_back(t1);
91+
Iv.push_back(t2);
92+
Iv.push_back(t3);
93+
Iv.push_back(t4);
94+
Iv.push_back(t5);
95+
// 3. 以修饰过的成员函数搭配stl算法
96+
for_each(Iv.begin(), Iv.end(), mem_fun_ref(&Int::print1));
97+
}

0 commit comments

Comments
 (0)