Results 1 to 8 of 8

Thread: Custom iterators, creation

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Custom iterators, creation

    As cprogramming.com is (as usual) too slow to be usable, I'll post this stuff here in the hope of inspiring discussion.

    The STL and Boost contain a few special standalone iterators. They don't iterator over containers, rather they perform a special function. Those containers are of input_iterator and output_iterator categories and include std::ostream_iterator (wonderful for copying an array to a stream), std::istream_iterator (the reverse), std::back_insert_iterator (append everything to the back of a container) as well as the relatives std::insert_iterator and std::front_insert_iterator and boost::filesystem::directory_iterator (iterate over the files of a directory).

    All these iterators aren't obtained by any begin() and end() functions, but created using constructors. For example, an ostream_iterator is created by passing a stream and a separation string:
    Code:
    std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
    For input iterators (those that can be read from), the "end" iterators are created by passing no arguments to the constructor:
    Code:
    std::vector<int> vec((std::istream_iterator<int>(std::cin)), std::isteam_iterator<int>());
    See also Item #6 in Effective STL for the exact syntax of this call.


    So far, so good.

    I've written three input iterators myself. One iterates over the drives (a:, c:, d:, ...). The second over the network shares (thanks Tech). The third over both by concatenating the two.

    The problem is, there isn't anything to initialize. All iterators have system-fixed iteration ranges and thus don't need to be passed any. Right now I have the default constructor creating a start iterator and a static method called end creating the end iterator (via a call to a private constructor that takes a dummy int argument).

    However, it doesn't feel quite right. Anybody got a better idea? I want this code to be perfect, I might submit it to Boost.
    Code:
    int index = 1;
    for(root_iterator it, end(root_iterator::end()); it != end; ++it) {
    	cout << index++ << ": ";
    	cout << it->string() << endl;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Is there anything else that uses iterators in the same fashion? Because you might be able to look at someone elses source to get some ideas.

    I think your right, dummy ints doesnt seem like the right thing to do. But I think you are a bit out of my leauge.

    You might also try posting this on Codeguru's forums. If you are not a member I would be more than happy to post it for you. There are some really good people in the C++ forums. If no one answers my question here, I usually get it answered there.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Do post it, please.

    I know of no other code that has such iterators unfortunatly...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok posted

    http://www.codeguru.com/forum/showth...hreadid=272841

    I will keep my eye on the email and let you know when there is a response.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Never mind, I'll post it myself, just joined.

    Oh, you already did.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Do you want me to delete it?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Nah, leave it there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Someone posted a response at codeguru
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width