[C++-sig] Re: iterator interface question (progress!)

David Abrahams dave at boost-consulting.com
Fri Jan 30 23:08:43 CET 2004


"Neal D. Becker" <nbecker at hns.com> writes:

> I have made some progress, I am really impressed with boost python capabilities!
>
>   typedef std::vector<double>::iterator vd_it;
>
>   class_<vd_it>("DVecIter")
>     ;
>
>   class_<std::vector<double> >("DVec")
>     .def(init<size_t>())
>     .def(vector_indexing_suite<std::vector<double> >())
>     .def("begin", (vd_it (std::vector<double>::*)())(&std::vector<double>::begin))
>     .def("end", (vd_it (std::vector<double>::*)())(&std::vector<double>::end))
>     ;
>
>   typedef std::vector<Complex>::iterator vc_it;
>
>   class_<vc_it>("CVecIter")
>     ;
>
>   class_<std::vector<Complex> >("CVec")
>     .def(init<size_t>())
>     .def(vector_indexing_suite<std::vector<Complex>, true >())
>     .def("begin", (vc_it (std::vector<Complex>::*)())(&std::vector<Complex>::begin))
>     .def("end", (vc_it (std::vector<Complex>::*)())(&std::vector<Complex>::end))
>     ;
>     [...]
>
> Just with this, my main goal is accomplished:
>
>>>> from Test import *
>>>> a = DVec (2)
>
>>>> for x in range (0, len (a)): a[x] = x
>>>> PrintV (a.begin(), a.end())
> 0
> 1

Better make begin and end functions use return_internal_reference, so
the iterators won't outlive the container.  UNfortunately, they're
still not safe since they can still run off the end of the vector or
become invalidated.  These are the sorts of issues that the indexing
suites should handle.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list