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/iterator/indirect_iterator.hpp>
14 int main(int, char*[])
16 char characters
[] = "abcdefg";
17 const int N
= sizeof(characters
)/sizeof(char) - 1; // -1 since characters has a null char
18 char* pointers_to_chars
[N
]; // at the end.
19 for (int i
= 0; i
< N
; ++i
)
20 pointers_to_chars
[i
] = &characters
[i
];
22 // Example of using indirect_iterator
24 boost::indirect_iterator
<char**, char>
25 indirect_first(pointers_to_chars
), indirect_last(pointers_to_chars
+ N
);
27 std::copy(indirect_first
, indirect_last
, std::ostream_iterator
<char>(std::cout
, ","));
28 std::cout
<< std::endl
;
31 // Example of making mutable and constant indirect iterators
33 char mutable_characters
[N
];
34 char* pointers_to_mutable_chars
[N
];
35 for (int j
= 0; j
< N
; ++j
)
36 pointers_to_mutable_chars
[j
] = &mutable_characters
[j
];
38 boost::indirect_iterator
<char* const*> mutable_indirect_first(pointers_to_mutable_chars
),
39 mutable_indirect_last(pointers_to_mutable_chars
+ N
);
40 boost::indirect_iterator
<char* const*, char const> const_indirect_first(pointers_to_chars
),
41 const_indirect_last(pointers_to_chars
+ N
);
43 std::transform(const_indirect_first
, const_indirect_last
,
44 mutable_indirect_first
, std::bind1st(std::plus
<char>(), 1));
46 std::copy(mutable_indirect_first
, mutable_indirect_last
,
47 std::ostream_iterator
<char>(std::cout
, ","));
48 std::cout
<< std::endl
;
51 // Example of using make_indirect_iterator()
53 std::copy(boost::make_indirect_iterator(pointers_to_chars
),
54 boost::make_indirect_iterator(pointers_to_chars
+ N
),
55 std::ostream_iterator
<char>(std::cout
, ","));
56 std::cout
<< std::endl
;