[C++-sig] Re: iterator interface question (progress!)
Neal Becker
ndbecker at verizon.net
Sat Jan 31 19:40:44 CET 2004
David Abrahams wrote:
> "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.
>
Thanks for the help, but I still have 1 question about your reply (the
meaning of "should"). Do you mean the indexing suites already do provide a
better solution (and I should look at it) or do you mean that you wish the
indexing suites provided a better solution, and may in the future?
More information about the Cplusplus-sig
mailing list