diff --git a/counting_iterator.htm b/counting_iterator.htm index cd8685e..2549634 100644 --- a/counting_iterator.htm +++ b/counting_iterator.htm @@ -95,7 +95,11 @@ int main(int, char*[]) // to be continued... - +The output from this part is: +
+counting from 0 to 4: +0 1 2 3 +
// continuing from previous example... - std::cout << "counting from -5 to 5:" << std::endl; + + std::cout << "counting from -5 to 4:" << std::endl; std::copy(boost::make_counting_iterator(-5), boost::make_counting_iterator(5), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; + // to be continued... ++The output from this part is: +
+counting from -5 to 4: +-5 -4 -3 -2 -1 0 1 2 3 4 +- return 0; -} +In the next example we create an array of numbers, and then create a +second array of pointers, where each pointer is the address of a +number in the first array. The counting iterator makes it easy to do +this since dereferencing a counting iterator that is wrapping an +iterator over the array of numbers just returns a pointer to the +current location in the array. We then use the indirect iterator adaptor to print +out the number in the array by accessing the numbers through the array +of pointers. + +
+ // continuing from previous example... + + const int N = 7; + std::vector<int> numbers; + // Fill "numbers" array with [0,N) + std::copy(boost::make_counting_iterator(0), boost::make_counting_iterator(N), + std::back_inserter(numbers)); + std::vector<int*> pointers; + + // Use counting iterator to fill in the array of pointers. + std::copy(boost::make_counting_iterator(numbers.begin()), + boost::make_counting_iterator(numbers.end()), + std::back_inserter(pointers)); + + // Use indirect iterator to print out numbers by accessing + // them through the array of pointers. + std::cout << "indirectly printing out the numbers from 0 to " + << N << std::endl; + std::copy(boost::make_indirect_iterator(pointers.begin()), + boost::make_indirect_iterator(pointers.end()), + std::ostream_iterator<int>(std::cout, " ")); + std::cout << std::endl; ++The output is: +
+indirectly printing out the numbers from 0 to 7 +0 1 2 3 4 5 6diff --git a/counting_iterator_example.cpp b/counting_iterator_example.cpp index 21d5fb0..67bbb70 100644 --- a/counting_iterator_example.cpp +++ b/counting_iterator_example.cpp @@ -6,7 +6,10 @@ #include