Boost logo

Boost Users :

From: shunsuke (pstade.mb_at_[hidden])
Date: 2007-02-19 13:10:43


abir basak wrote:
> shunsuke wrote:
>>> So there are 3 library I am searching,
>>> 1) Index -> iterator , and iterator -> index conversion
>>> (I know that 2 famous facts in STL that iterator doesn't know container
>>> and their position, but that is only applicable for pointers . There is
>>> no harm to know this facts from iterator. and thus some library can do
>>> to-from conversion between iterator & index position , or better
>>> sub_range & pair<index_t,index_t> any library ?
>> Boost has 'counting_iterator'.
>> http://www.boost.org/libs/iterator/doc/counting_iterator.html
>> You can call 'counting_iterator::base()' if you want the 'index'.
>>
> Here I am not getting the "index" from counting_iterator.
> std::vector<int> v;
> v+= 1,2,3,4,5,6,7,8,9,10;
> std::for_each(v.begin(),v.end(),std::cout<<_1<<" ");
> counting_iterator<std::vector<int>::iterator > it_b =
> make_counting_iterator(v.begin()+3);
> std::cout<<*it_b.base(); /// This prints the element at that pos i.e 4,
> not the position, i.e 3 . How to get the index back from iterator ?

Boost doens't have such iterator, AFAIK.
You maybe need to write one from scratch, holding 'v.begin()' or something.

>>> 3) an utility which allows to refer a portion (or some portions ) of a
>>> container to be stored ( i.e store by index & return by iterator )
>>> inside another class , using the above two library ....
>> If you take the portions represented by different types into
>> a single deque, Dr.Becker's 'any_iterator' is a candidate.
>> http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/start_page.html
> At present I don't need a polymorphic or heterogeneous iterator. I am
> looking at it.

You try to use an iterator through the "index".
That is a polymorphism. :-)

> And also looking at some iterator adapter (or range ) which can be
> stored inside a class or another container. i.e it will perform all of
> the operations using index & a reference to the container internally.
> Thus it needs to know the container & the position in the container. can
> any_iterator or some other be stored (or boost::sub_range of those
> iterator adapter) inside a class , or I need to store index and
> construct iterator with the help of container.

After all, the index seems to behave as the type erasure.
'any_iterator' is the most generic one to do it.
(You may recall a battle of switch vs virtual.)
On the other hand, it is difficult to implement 'any_iterator'.
An iterator tracking its own index can be a reasonable solution.

Regards,

-- 
Shunsuke Sogame

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