Thread: Connecting input iterator to output iterator

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    Connecting input iterator to output iterator

    Ok, so I have this code:

    Code:
    template <typename Iter, typename Ins>
    bit8_to_ASCII (Iter begin, Iter end, Ins output) {
       // Need this vector to hold intermediate values.
       std::vector<unsigned char> bit6;
    
       // Stretch the 8-bit values out to 6-bit values, store the result in vector bit6.
       bit8_to_bit6 (begin, end, std::back_inserter(bit6));
    
       // Translate the 6-bit values into ASCII values.  Store the output in the
       //   output stream.
       std::transform (bit6.begin(), bit6.end(), output, bit6_to_ASCII);
    
       return;
    }
    Conceptually... vector bit6 is really not needed. It's just an artifact of the problem that I can't start std::transform until after the bit8_to_bit6 function is done. Is there some kind of inserter-wrapper I can use to take it out of the picture? Something like this maybe:

    Code:
    template <typename Iter, typename Ins>
    bit8_to_ASCII (Iter begin, Iter end, Ins output) {
       // No need to allocate an intermediate vector
    
       // Whenever something is output to transforming_inserter, it calls bit6_to_ASCII
       //   on the input, and stores the result in output.
       SOME_TYPE transforming_inserter = SOME_FUNCTION(output, bit6_to_ASCII);
    
       // Stretch the 8-bit values out to 6-bit values, store the result in 
       //   the custom inserter which will perform bit6_to_ASCII on the fly.
       bit8_to_bit6 (begin, end, transforming_inserter);
    
       return;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have a look at the boost.iterators library. I haven't used it much myself, but I think transform_iterator might be able to help you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    transform_iterator does 1:1 transformations. I don't think there's a proper pre-made iterator for your purposes. It's not that hard to write one, though. Using the Boost.Iterators facade, you can cook up an iterator pretty quickly. You should have some experience in writing easier iterators first, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  3. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM
  4. file input and output
    By isaac in forum C Programming
    Replies: 3
    Last Post: 06-04-2002, 04:41 PM
  5. Multi input to Output.
    By Krullt in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 02:07 PM