1 // (C) Copyright Jeremy Siek 2000-2004.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/config.hpp>
12 #include <boost/bind/bind.hpp>
13 #include <boost/iterator/indirect_iterator.hpp>
15 int main(int, char*[])
17 char characters
[] = "abcdefg";
18 const int N
= sizeof(characters
)/sizeof(char) - 1; // -1 since characters has a null char
19 char* pointers_to_chars
[N
]; // at the end.
20 for (int i
= 0; i
< N
; ++i
)
21 pointers_to_chars
[i
] = &characters
[i
];
23 // Example of using indirect_iterator
25 boost::indirect_iterator
<char**, char>
26 indirect_first(pointers_to_chars
), indirect_last(pointers_to_chars
+ N
);
28 std::copy(indirect_first
, indirect_last
, std::ostream_iterator
<char>(std::cout
, ","));
29 std::cout
<< std::endl
;
32 // Example of making mutable and constant indirect iterators
34 char mutable_characters
[N
];
35 char* pointers_to_mutable_chars
[N
];
36 for (int j
= 0; j
< N
; ++j
)
37 pointers_to_mutable_chars
[j
] = &mutable_characters
[j
];
39 boost::indirect_iterator
<char* const*> mutable_indirect_first(pointers_to_mutable_chars
),
40 mutable_indirect_last(pointers_to_mutable_chars
+ N
);
41 boost::indirect_iterator
<char* const*, char const> const_indirect_first(pointers_to_chars
),
42 const_indirect_last(pointers_to_chars
+ N
);
44 std::transform(const_indirect_first
, const_indirect_last
,
45 mutable_indirect_first
, boost::bind(std::plus
<char>(), 1, boost::placeholders::_1
));
47 std::copy(mutable_indirect_first
, mutable_indirect_last
,
48 std::ostream_iterator
<char>(std::cout
, ","));
49 std::cout
<< std::endl
;
52 // Example of using make_indirect_iterator()
54 std::copy(boost::make_indirect_iterator(pointers_to_chars
),
55 boost::make_indirect_iterator(pointers_to_chars
+ N
),
56 std::ostream_iterator
<char>(std::cout
, ","));
57 std::cout
<< std::endl
;