iterators and views of lists
Alf P. Steinbach
alfps at start.no
Fri Dec 18 15:18:04 EST 2009
* Carl Banks:
> On Dec 18, 11:08 am, "Alf P. Steinbach" <al... at start.no> wrote:
>> * Carl Banks:
>>
>>
>>
>>> On Dec 17, 10:00 pm, Brendan Miller <catph... at catphive.net> wrote:
>>>> On Thu, Dec 17, 2009 at 6:44 PM, Steven D'Aprano
>>>> <st... at remove-this-cybersource.com.au> wrote:
>>>>> On Thu, 17 Dec 2009 12:07:59 -0800, Brendan Miller wrote:
>>>>>> I was thinking it would be cool to make python more usable in
>>>>>> programming competitions by giving it its own port of the STL's
>>>>>> algorithm library, which needs something along the lines of C++'s more
>>>>>> powerful iterators.
>>>>> For the benefit of those of us who aren't C++ programmers, what do its
>>>>> iterators do that Python's don't?
>>>> Python iterators basically only have one operation:
>>>> next(), which returns the next element or throws StopIteration.
>>>> In C++ terminology this is a Input iterator. It is good for writing
>>>> "for each" loops or map reduce operations.
>>> Hmm. I guess the main thing that's bothering me about this whole
>>> thread is that the true power of Python iterators is being overlooked
>>> here, and I don't think you're being fair to call Python iterators
>>> "weak" (as you did in another thread) or say that they are only good
>>> for for-else type loops.
>>> The fact is, Python iterators have a whole range of powers that C++
>>> iterators do not. C++ iterators, at least the ones that come in STL,
>>> are limited to iterating over pre-existing data structures. Python
>>> iterators don't have this limation--the data being "iterated" over can
>>> be virtual data like an infinite list, or data generated on the fly.
>>> This can be very powerful.
>> It's good that Python iterators can do things.
>>
>> However, it's not the case that C++ iterators can't do those things.
>>
>> C++ iterators very much routinely do such things.
>
> STL Iterators? No. Maybe if you're talking about boost or homemade
> iterators or something I could buy it, though I'd still bs suspicious
> of "routinely".
The intersection of STL and the C++ standard library is of nearly the same size
as the STL, but I don't understand why you're limiting yourself to the STL.
After all, the STL was first developed for the Ada language, IIRC.
Using the standard C++ library, per the 1998 first (and current!) C++ standard,
below is an idiomatic "copy Carl Banks to output" C++ program where, you might
note, the "ostream_iterator" has a sister "istream_iterator" that definitely
does not only iterat over a "pre-existing data structure", as you wrote.
Of course, mostly one defines iterators in the source code or by using Boost or
similar libraries; that's how C++ works.
There's no special language support for iterators in C++; it's wholly a library
and application defined concept.
<code>
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main()
{
using namespace std;
static char const* const words[] =
{ "Carl", "Banks", "is", "wrong", "on", "the", "Internet", "!" };
copy(
words, words + sizeof(words)/sizeof(*words),
ostream_iterator<char const*>( cout, "\n" )
);
}
</code>
Cheers & hth.,
- Alf
More information about the Python-list
mailing list