-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathiterator.projection.cpp
More file actions
96 lines (77 loc) · 2.28 KB
/
iterator.projection.cpp
File metadata and controls
96 lines (77 loc) · 2.28 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <boost/iterator/transform_iterator.hpp>
using namespace std;
using namespace boost;
struct personnel_record
{
personnel_record(string n, int id) : m_name(n), m_ID(id) {}
string m_name;
int m_ID;
};
struct select_name
{
typedef personnel_record argument_type;
typedef string const& result_type;
const string& operator()(const personnel_record& r) const
{
return r.m_name;
}
string& operator()(personnel_record& r) const
{
return r.m_name;
}
};
struct select_ID
{
typedef personnel_record argument_type;
typedef int& result_type;
const int& operator()(const personnel_record& r) const
{
return r.m_ID;
}
int& operator()(personnel_record& r) const
{
return r.m_ID;
}
};
ostream_iterator<string> sout(cout, "\n");
ostream_iterator<int> iout(cout, "\n");
int main(int, char*[])
{
list<personnel_record> personnel_list;
personnel_list.push_back(personnel_record("Barney", 13423));
personnel_list.push_back(personnel_record("Fred", 12343));
personnel_list.push_back(personnel_record("Wilma", 62454));
personnel_list.push_back(personnel_record("Betty", 20490));
// Example of using transform_iterator to print out the names in the
// personnel list using a projection.
transform_iterator<select_name , list<personnel_record>::iterator>
personnel_first(personnel_list.begin()),
personnel_last (personnel_list.end());
copy(personnel_first, personnel_last, sout);
cout << endl;
// Example of using transform_iterator with const_iterators to
// assign new ID numbers to the personnel.
transform_iterator<select_ID, list<personnel_record>::iterator>
ID_first(personnel_list.begin()),
ID_last(personnel_list.end());
int new_id = 0;
while (ID_first != ID_last) {
*ID_first = new_id++;
++ID_first;
}
transform_iterator<select_ID, list<personnel_record>::const_iterator, int const& >
const_ID_first(personnel_list.begin()),
const_ID_last(personnel_list.end());
copy(const_ID_first, const_ID_last, iout);
cout << endl;
cout << endl;
copy(make_transform_iterator<select_name>(personnel_list.begin()) ,
make_transform_iterator<select_name>(personnel_list.end()) ,
sout);
return 0;
}