Brian Silverman | 5962333 | 2018-08-04 23:36:56 -0700 | [diff] [blame^] | 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) |
| 5 | |
| 6 | #include <boost/config.hpp> |
| 7 | #include <vector> |
| 8 | #include <iostream> |
| 9 | #include <iterator> |
| 10 | #include <functional> |
| 11 | #include <algorithm> |
| 12 | #include <boost/iterator/indirect_iterator.hpp> |
| 13 | |
| 14 | int main(int, char*[]) |
| 15 | { |
| 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]; |
| 21 | |
| 22 | // Example of using indirect_iterator |
| 23 | |
| 24 | boost::indirect_iterator<char**, char> |
| 25 | indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N); |
| 26 | |
| 27 | std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ",")); |
| 28 | std::cout << std::endl; |
| 29 | |
| 30 | |
| 31 | // Example of making mutable and constant indirect iterators |
| 32 | |
| 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]; |
| 37 | |
| 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); |
| 42 | |
| 43 | std::transform(const_indirect_first, const_indirect_last, |
| 44 | mutable_indirect_first, std::bind1st(std::plus<char>(), 1)); |
| 45 | |
| 46 | std::copy(mutable_indirect_first, mutable_indirect_last, |
| 47 | std::ostream_iterator<char>(std::cout, ",")); |
| 48 | std::cout << std::endl; |
| 49 | |
| 50 | |
| 51 | // Example of using make_indirect_iterator() |
| 52 | |
| 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; |
| 57 | |
| 58 | return 0; |
| 59 | } |