|
-
Sep 17th, 2002, 12:03 PM
#1
STL iterators
Suppose I want to have itertors for my own class.
In particular, I have a class with the purpose of resolving wildcarded filenames for me (searches the filesystem for any files that match, stores them internally).
Is there a way I can make the class expose an output iterator even if I don't have an STL container internally? So I can do:
vector<string> vs;
CFileGetter fg(file, bRecursive);
copy(fg.begin(), fg.end(), back_inserter(vs));
Thx in advance.
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.
-
Sep 17th, 2002, 12:15 PM
#2
Frenzied Member
Thats the whole point of the STL. You can provide your own iterators for your own container classes, and you can still use them compatibly. In fact, it is recommended that you do.
Z.
-
Sep 17th, 2002, 01:07 PM
#3
Great.
How?
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.
-
Sep 17th, 2002, 02:57 PM
#4
Frenzied Member
Well, what is your internal structure? Say a raw pointer...
Code:
template<class t>
class F_Iterator
{
public:
F_Iterator& operator ++(){m_pos_ptr++;return *this;}
F_Iterator& operator --(){m_pos_ptr--;return *this;}
t& operator *(){return *m_pos_ptr;}
t& operator *() const{return *m_pos_ptr;}
private:
t* m_pos_ptr;
};
Should work. Make that an inner class of your container class... then...
Code:
F_Iterator<t> YourClass<t>::begin()
{
F_Iterator<t> i;
i.m_pos_ptr = myStuff;
return i;
}
F_Iterator<t> YourClass<t>::end()
{
F_Iterator<t> i;
i.m_pos_ptr = myStuff + mySize;
return i;
}
Should work...
Z.
-
Sep 17th, 2002, 03:13 PM
#5
In what way is that compatible to the other STL iterators?
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.
-
Sep 17th, 2002, 05:53 PM
#6
Monday Morning Lunatic
Compatible by concept.
That is, it can be substituted in a template, therefore it must supply certain features, things like * to dereference, ++/-- to navigate, things like that.
They don't need *all* of those, but you'll find which ones you need when you try and use them.
If it looks like an STL iterator when reading the code, you're on a good track.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 17th, 2002, 08:59 PM
#7
Frenzied Member
The above code needs a couple of boolean operators as well (specifically, != would be a needed one).
Z.
-
Sep 18th, 2002, 03:04 AM
#8
Can I pass such an iterator to the algorithms?
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.
-
Sep 18th, 2002, 03:45 AM
#9
Frenzied Member
As long as you provide the operators that the algorithm function requires. Remember, since all of the algorithms are templated functions, the code is provided with the compiler. The templates are compiled along with your own code, and as long as the template algorithm functions try to call functions and operators that you have provided, you are fine.
Z.
-
Sep 18th, 2002, 05:53 AM
#10
Hmmmmm....
Thanks.
Thing is, I'm writing classes that encapsulate POSIX file system handling, and I need iterators to enumerate the directory entries.
It's just a quick hack currently, but I want to upgrade it to full STL requirements compliance later.
Last edited by CornedBee; Sep 18th, 2002 at 05:57 AM.
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.
-
Sep 18th, 2002, 06:44 AM
#11
Monday Morning Lunatic
See the Filesystem library at www.boost.org 
I think so far it's only in the sandbox CVS, but it should appear in the 1.29 release...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 18th, 2002, 05:28 PM
#12
thx parksie, will take a look tomorrow - actually today (damn I'm tired)
Ok, found some info (mainly by browsing the list and iterator headers).
Whatever class you use as iterator (I use a class because of the very special ++ operation I need to perform), it must contain a few typedefs in order to get iterator_traits working:
typedef some_tag iterator_category;
where some_tag may be any of
input_iterator_tag
output_iterator_tag
forward_iterator_tag
bidirectional_iterator_tag
random_access_iterator_tag
pointers are classified as random access iterators
typedef foo value_type;
where foo is is the type of (*iterator)
typedef value_type *pointer;
typedef value_type &reference;
unless you use some special kind of pointer
typedef bar difference_type;
where bar is a type capable of expressing the difference between two iterators. For pointers this defaults to ptrdiff_t
There was another one, but I forgot...
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.
-
Sep 19th, 2002, 08:53 AM
#13
There is no filesystem library in the list at boost.org...
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.
-
Sep 19th, 2002, 08:59 AM
#14
Monday Morning Lunatic
You need the sandbox, it's not going in until 1.29.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 19th, 2002, 09:38 AM
#15
you mean January 1st?
Can't wait that long, needs to be finished in at most two weeks
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.
-
Sep 19th, 2002, 09:43 AM
#16
Monday Morning Lunatic
You can download it from their CVS repository.
No wait, you're on Windows
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 19th, 2002, 10:00 AM
#17
It's for UNIX, so I can. But I don't know how. Maybe you can help?
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.
-
Sep 19th, 2002, 10:07 AM
#18
Monday Morning Lunatic
They have the login and download instructions on their site. You need to build their build tool as well though :-/
It's a bit complex because you have to merge the two together.
Ah well. Don't bother and have a play when it gets released
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 23rd, 2002, 07:40 PM
#19
Frenzied Member
Well, here is a nifty little tidbit I discovered =). Raw pointers seem to be perfectly valid "iterators" to be passed to the various <algorithm> functions =).
Z.
-
Sep 23rd, 2002, 07:53 PM
#20
Monday Morning Lunatic
Yep. I think that was one of the original goals of them 
It took me a while to find that as well, but when I did, my life got easier
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 23rd, 2002, 08:43 PM
#21
Frenzied Member
Im sure =). I was playing around with the allocator class, and it just sort of popped into my head... so i tried it =).
Z.
-
Sep 24th, 2002, 04:04 AM
#22
There is a specialization of the iterator_traits struct for any pointer, this is why it works. They are always classified as random access, with ptrdiff_t being the difference type.
Most STL vector implementations use raw pointers as iterators.
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.
-
Sep 24th, 2002, 05:48 AM
#23
Frenzied Member
Been playing with allocators too. Load of fun =).
Z.
-
Sep 24th, 2002, 05:51 AM
#24
Haven't looked into allocators yet, what can you use them for?
But I found an interesting article about creating a string class with case-independent comparison as a specialisation of basic_string. Can't remember where though...
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.
-
Sep 24th, 2002, 05:56 AM
#25
Frenzied Member
Well, the default allocator will simply use new, and give you a chunk of memory, but you can overlaod that behavior. For instance, last night, I overloaded the allocate and deallocate functions so that the memory returned was a part of a static memory block (ie, all objects are created in the same block of memory. Since all of the STL objects can take allocator parameters, you could use this so that your vector, map, list, etc, etc, all took memory where you specify.
Lots of fun =).
Z.
-
Sep 24th, 2002, 06:01 AM
#26
Very cool. STL rocks, and as Bjarne Strousrup says, we all have yet to learn the STL.
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.
-
Sep 24th, 2002, 11:04 AM
#27
Frenzied Member
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
|