Topic: reversible_containers and iterator arithmetic


Author: "Joseph Gottman" <joegottman@worldnet.att.net>
Date: 2000/06/18
Raw View
Joseph M. O'Leary <jmoleary@earthlink.net> wrote in message
news:f9l25.151$FC6.7630@newsread1.prod.itd.earthlink.net...
> According to the table in section 23.1 paragraph 5, the expression
>
>      a.size()
>
>  has the operational semantics of
>
>      a.end() - a.begin()
>
> Now suppose I have a reversible, random-access container like a vector.
May
> I infer that (for a non-empty vector) instead of doing:
>
>      *a.rbegin()
>
> I can instead legally do this:
>
>     *(a.end() - 1)
>
> Is the second expression guaranteed to be valid for such a container?
>
>

    No.  If the container is empty (so begin() == end()) then this might
cause the program to crash.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/06/18
Raw View
Joseph Gottman wrote:
>
> Joseph M. O'Leary <jmoleary@earthlink.net> wrote in message
> news:f9l25.151$FC6.7630@newsread1.prod.itd.earthlink.net...
> > According to the table in section 23.1 paragraph 5, the expression
> >
> >      a.size()
> >
> >  has the operational semantics of
> >
> >      a.end() - a.begin()
> >
> > Now suppose I have a reversible, random-access container like a vector.
> May
> > I infer that (for a non-empty vector) instead of doing:
                        ^^^^^^^^^
> >
> >      *a.rbegin()
> >
> > I can instead legally do this:
> >
> >     *(a.end() - 1)
> >
> > Is the second expression guaranteed to be valid for such a container?
> >
> >
>
>     No.  If the container is empty (so begin() == end()) then this might
> cause the program to crash.

He specified a non-empty vector.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Joseph M. O'Leary" <jmoleary@earthlink.net>
Date: 2000/06/17
Raw View
According to the table in section 23.1 paragraph 5, the expression

     a.size()

 has the operational semantics of

     a.end() - a.begin()

Now suppose I have a reversible, random-access container like a vector.  May
I infer that (for a non-empty vector) instead of doing:

     *a.rbegin()

I can instead legally do this:

    *(a.end() - 1)

Is the second expression guaranteed to be valid for such a container?



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]