From d960e5eaddc04b27c7ebe000e938289dcb14272a Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 12 Feb 2001 21:57:19 +0000 Subject: [PATCH] added another example [SVN r9177] --- counting_iterator.htm | 58 ++++++++++++++++++++++++++++++++--- counting_iterator_example.cpp | 26 +++++++++++++++- 2 files changed, 78 insertions(+), 6 deletions(-) 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 
+

Template Parameters

@@ -118,7 +122,7 @@ If the Incrementable type has all of the functionality of a Access Iterator except the operator*(), then the counting iterator will be a model of Random -Access Iterator. In the Incrementable type has less +Access Iterator. If the Incrementable type has less functionality, then the counting iterator will have correspondingly less functionality. @@ -157,15 +161,59 @@ typing.
   // 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 6 
 
diff --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 #include +#include +#include #include +#include int main(int, char*[]) { @@ -17,12 +20,33 @@ int main(int, char*[]) std::cout << std::endl; // Example of using make_counting_iterator() - 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(std::cout, " ")); std::cout << std::endl; + // Example of using counting iterator to create an array of pointers. + const int N = 7; + std::vector 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 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(std::cout, " ")); + std::cout << std::endl; + return 0; }