-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindirect.cpp
More file actions
49 lines (38 loc) · 1.41 KB
/
indirect.cpp
File metadata and controls
49 lines (38 loc) · 1.41 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
#include <boost/iterator/indirect_iterator.hpp>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <algorithm>
using namespace std;
using namespace boost;
ostream_iterator<char> out(cout, ",");
int main(int, char*[])
{
char characters[] = "abcdefg";
const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
char* pointers_to_chars[N]; // at the end.
for (int i=0; i<N; ++i)
pointers_to_chars[i] = &characters[i];
// Example of using indirect_iterator
indirect_iterator<char**, char> ifirst(pointers_to_chars), ilast(pointers_to_chars + N);
copy(ifirst, ilast, out);
cout << endl;
// Example of making mutable and constant indirect iterators
char mutable_characters[N];
char* pointers_to_mutable_chars[N];
for (int j=0; j<N; ++j) pointers_to_mutable_chars[j] = &mutable_characters[j];
indirect_iterator<char* const*>
mifirst(pointers_to_mutable_chars), milast(pointers_to_mutable_chars + N);
indirect_iterator<char* const*, char const>
cifirst(pointers_to_chars), cilast(pointers_to_chars + N);
transform(cifirst, cilast, mifirst, bind1st(plus<char>(), 1));
copy(mifirst, milast, out);
cout << endl;
// Example of using make_indirect_iterator()
copy(make_indirect_iterator(pointers_to_chars),
make_indirect_iterator(pointers_to_chars + N),
out);
cout << endl;
return 0;
}