|
Boost Users : |
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2008-03-10 06:09:05
Ashkan Aliabadi <ashkan.aliabadi <at> gmail.com> writes:
>
>
> Following node_iter example of iterator_facade (here), how can I wrap that
inside an any_iterator? I'm really having a hard time figuring that out. I'm
using an any_iterator implementation by Thomas Becker (here). I would really
appreciate it if anyone could give me a hand.Regards
You should use iterator_facade to create new types of iterators (for example, if
you have your own data structure and want to provide iterator based access to
its elements). This is one part of the story. Another part is any_iterator. It's
just an iterator that any other iterator can be convertible to. You don't need
to add support for any_iterator in your own iterator (that you probably create
using iterator_facade); any_iterator will just work with any iterator type (even
ones from STL).
Here is an example of how you can use any_iterator.
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <any_iterator.hpp>
int main()
{
// first type argument is value_type of
// iterator. second is iterator category.
typedef IteratorTypeErasure::any_iterator
<int, std::random_access_iterator_tag>
random_access_iterator_of_int;
typedef IteratorTypeErasure::any_iterator
<int, std::input_iterator_tag>
input_iterator_of_int;
std::vector<int> v;
// any random access iterator with value_type = int
// is convertible to random_access_iterator_of_int
random_access_iterator_of_int r(v.begin());
// any input iterator with value_type = int
// is convertible to input_iterator_of_int
input_iterator_of_int b(v.begin()), e(v.end());
std::copy(b, e, std::ostream_iterator<int>(std::cout, "\n"));
}
HTH,
Roman Perepelitsa
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net