comp.lang.ada
 help / color / mirror / Atom feed
From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: Look what I caught! was re:Ada paper critic
Date: Wed, 19 Jun 2002 16:02:46 -0400
Date: 2002-06-19T16:02:46-04:00	[thread overview]
Message-ID: <3D10E366.2010303@mail.com> (raw)
In-Reply-To: wcc7kkv5amf.fsf@shell01.TheWorld.com

Robert A Duff wrote:
> "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net> writes:
>>Also, the standard only requires a valid pointer to the point just after the
>>end of an array; no similar rule applies to the beginning of the array. For
>>example, if you declare
>>    PLANET *before_mercury = solar_system - 1;
>>then the standard does NOT require that before_mercury compare meaningfully
>>with the addresses of the solar_system elements.
> 
> Really?  I thought the above was OK.  The STL of C++ depends heavily on
> this idiom.  Is this a difference between C and C++?  If so, what's the
> rationale (in C)?

It's not OK, and the STL never uses this idiom. The
STL uses half-open intervals [begin,end) where the
begin iterator points to the start of the sequence
being manipulated, and the end iterator points to
one past the end of the sequence being manipulated,
and the canonical STL algorithm loop is

	while (begin != end) { operate_on(*begin++); }

The "one past the end" pointer can be logical instead
of physical, depending on the underlying container, but
the algorithms don't care. For example, suppose I wish
to copy a sequnce of integers from standard input into
a vector. I can do it this way

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> v;
	copy(
	/* begin */ istream_iterator<int>(cin),
	/* end   */ istream_iterator<int>(),
	/* to    */ back_insert_iterator(v)
	);
}

The istream_iterator<int>() reprsents a logical past-the-end
iterator for reading from an input stream. A real input stream
iterator will compare equal to that one once it has tried to
read past the end-of-file on the input stream.

The code one writes for something like copy (copy itself is
part of the standard library) is completely ignorant of these
shenaningans. It looks like a simple pointer loop -

	template<typename II, typename OI>
	OI copy(II begin, II end, OI to)
	{
		while (begin != end)
			*to++ = *begin++;
		return to;
	}

The various iterator types have overloaded operators which
implement their behavior. For example, incrementing an
istream_iterator causes it to read the next object from
the input stream. Dereferencing the iterator returns that
object.




  reply	other threads:[~2002-06-19 20:02 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-17 17:35 Look what I caught! was re:Ada paper critic Alderson, Paul A.
2002-06-17 18:31 ` Darren New
2002-06-17 21:40 ` Vinzent Hoefler
2002-06-17 23:14   ` Darren New
2002-06-18 14:49     ` Hyman Rosen
2002-06-18 22:36     ` Vinzent Hoefler
2002-06-18 13:28   ` Marin David Condic
2002-06-24 19:17     ` Vinzent Hoefler
2002-06-18 19:16   ` Kevin Cline
2002-06-18 22:36     ` Vinzent Hoefler
2002-06-19 14:29       ` Wes Groleau
2002-06-19 16:59         ` Darren New
2002-06-19 17:48           ` Wes Groleau
2002-06-19 17:56             ` Darren New
2002-06-19 17:11         ` Frank J. Lhota
2002-06-19 19:31           ` Robert A Duff
2002-06-19 20:02             ` Hyman Rosen [this message]
2002-09-24 15:23               ` Matthew Heaney
2002-06-19 19:37         ` Robert A Duff
2002-06-19 13:52 ` Ted Dennison
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox