|
-
Nov 19th, 2003, 04:12 PM
#1
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.
-
Nov 19th, 2003, 05:39 PM
#2
Frenzied Member
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

-
Nov 19th, 2003, 05:40 PM
#3
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.
-
Nov 19th, 2003, 05:44 PM
#4
Frenzied Member
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

-
Nov 19th, 2003, 05:44 PM
#5
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.
-
Nov 19th, 2003, 05:45 PM
#6
Frenzied Member
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

-
Nov 19th, 2003, 05:53 PM
#7
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.
-
Nov 19th, 2003, 06:41 PM
#8
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|