|
-
November 19th, 2003, 05:43 PM
#1
Custom iterators, creation
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. 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;
}
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
|