[C++-sig] Re: iterator interface question

Neal D. Becker nbecker at hns.com
Fri Jan 30 18:53:36 CET 2004


David Abrahams wrote:

> "Neal D. Becker" <nbecker at hns.com> writes:
> 
>> My question is really generic.  I have lots of C++ algorithms written in
>> STL
>> style.  They pass containers as pairs of iterators.
>>
>> Consider the generic algorithm X:
>>
>> template<typename in_t, typename out_t>
>> void X (in_t in, in_t inend, out_t out);
>>
>> Now I have exposed std::vector<double> (for example) to python, so python
>> can create a std::vector<double> like:
>>
>> x = DVec (10)
>>
>> But what I want to know, how can python call algorithm X, which expects a
>> pair of iterators?
>>
>> Since STL became widely used, it is increasingly common to write
>> algorithms in this style, so I hope there is a way to do this.
> 
> Again, what do you want to be able to pass to the algorithm?
> 
> e.g., any Python sequence, just a vector<double>, a Python iterator,
> etc.
> 
> There's no one accepted idiom for this across the Python/C++ boundary.
> 

Oh, OK.

At minimum, I want to pass vector<double>,  maybe like this:


x = DVec (10)
y = CppAlg (x.begin(), x.end())

To be really useful, it would be nice to be able to do more.
Hopefully, python would be able to modify the iterators being passed, so
something equivalent to :

x = DVec(10)
y = CppAlg (x.begin()+1, x.end())

would be possible.  Or perhaps:

y = CppAlg (make_strided_iterator (x.begin(), 2), make_strided_iterator
(x.end(), 2))

would be possible, although I have no idea how this would work.







More information about the Cplusplus-sig mailing list