-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl_iterator.cpp
185 lines (142 loc) · 4.01 KB
/
stl_iterator.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "stl_iterator.h"
FileEditor::FileEditor(string filename){
ifstream in(filename);
string line;
while (getline(in, line))
push_back(line);
}
void FileEditor::write(ostream & out){
for (iterator w = begin(); w != end(); w++)
out << *w << endl;
}
void fileEditor_test(){
string fname = "/home/dell/Downloads/DSA/cpp/text.txt";
int i = 1;
FileEditor f(fname);
FileEditor::iterator w = f.begin();
while (w != f.end()){
ostringstream ss;
ss << i++;
*w = ss.str() + ": " + *w;
w++;
}
f.write();
}
Z::Z(int ii):i(ii){
}
void Z::g(){
i++;
}
ostream& operator<<(ostream& os, const Z& z){
return os << z.i;
}
template<class Cont, class PtrMemFun> void apply(Cont& c, PtrMemFun f){
typename Cont::iterator it = c.begin();
while (it != c.end()){
//(it->*f)();
((*it).*f)();
it++;
}
}
void iterator_test(){
ostream_iterator<Z> out(cout, " ");
vector<Z> vz;
for (int i =0; i < 10; i++)
vz.push_back(Z(i));
copy(vz.begin(), vz.end(), out);
cout << endl;
apply(vz, &Z::g);
copy(vz.begin(), vz.end(), out);
cout << endl;
}
void reversable_iterator_test(){
ifstream in("/home/dell/Downloads/DSA/cpp/templates_learning.h");
string line;
vector<string> lines;
while(getline(in, line))
lines.push_back(line);
vector<string>::reverse_iterator r;
for (r = lines.rbegin(); r != lines.rend(); r++)
cout << *r << endl;
}
int a[]={1, 3, 5, 7, 11, 13, 17, 19, 23};
template <class cont> void frontInsertion(cont& ci){
copy(a, a+sizeof(a)/sizeof(a[0]), front_inserter(ci));
copy(ci.begin(), ci.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
template <class cont> void backInsertion(cont& ci){
copy(a, a+sizeof(a)/sizeof(a[0]), back_inserter(ci));
copy(ci.begin(), ci.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
template <class cont> void midInsertion(cont& ci){
typename cont::iterator it = ci.begin();
it++; it++; it++;
copy(a, a+sizeof(a)/(sizeof(a[0])*2), inserter(ci, it));
copy(ci.begin(), ci.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
void insert_diff_position_test(){
deque<int> di;
list<int> li;
vector<int> vi;
cout << "front insertion" << endl;
frontInsertion(di);
frontInsertion(li);
//frontInsertion(vi);
di.clear();
li.clear();
//vi.clear();
cout << "back insertion" << endl;
backInsertion(di);
backInsertion(li);
backInsertion(vi);
cout << "mid insertion" << endl;
midInsertion(di);
midInsertion(li);
midInsertion(vi);
}
void io_stream_iterator_test(){
ifstream in("/home/dell/Downloads/DSA/cpp/stl_iterator.h");
istream_iterator<string> init(in), end;
ostream_iterator<string> out(cout, "\n");
vector<string> vs;
copy(init, end, back_inserter(vs));
copy(vs.begin(), vs.end(), out);
*out++ = vs[0];
*out++ = "That's all, folks!";
}
void io_stream_buf_iterator_test(){
ifstream in("/home/dell/Downloads/DSA/cpp/stl_iterator.h");
istreambuf_iterator<char> isb(in), end;
ostreambuf_iterator<char> osb(cout);
while(isb != end)
*osb++ = *isb++;
cout << endl;
ifstream in2("/home/dell/Downloads/DSA/cpp/stl_iterator.h");
istreambuf_iterator<char> is(in2), end2;
ostreambuf_iterator<char> os(cout);
while (is != end2)
*os++ = *is++;
cout << endl;
}
void storage_iterator_test(){
const int quantity = 10;
Noisy* np = (Noisy*) new char[quantity * sizeof(Noisy)];
raw_storage_iterator<Noisy*, Noisy> rsi(np);
for (int i = 0; i < quantity; i++)
*rsi++ = Noisy();
cout << endl;
copy(np, np+quantity, ostream_iterator<Noisy>(cout, " "));
cout << endl;
for (int i = 0; i < quantity; i++)
(&np[j]->~Noisy();
delete (char*) np;
}
int main(){
iterator_test();
reversable_iterator_test();
insert_diff_position_test();
io_stream_iterator_test();
}