Date: prev next · Thread: first prev next last
2011 Archives by date, by thread · List index


On Fri, 2011-02-18 at 09:11 +0100, David Tardon wrote:
On Thu, Feb 17, 2011 at 11:36:27PM +0100, Jan Holesovsky wrote:
The main thing that I changed was that in some cases you used
vector<...>::iterator it = another_iterator + 1;  This is not correct,
operator+() is not defined on std::vector, so the result might be not
what you'd expect; similarly with -1.  I have changed those to ++it, or
--it (on a temporary copy, or so).

Hi, Kendy,

sure it is defined, see ISO C++, 24.1.5, table 76: Random access
iterator requirements. ISO C++, 23.1.1, table 68 even describes
operational semantics of operator[] for vector and deque by
*(a.begin() + n) :)

It works as long as the iterator is random access iterator.  As soon as
you change the container to, say, list, whose iterator is bi-directional
but not random access that above statement is not guaranteed to work.
(It may work, but not guaranteed.)

It's better (and safer) to use std::advance to advance the iterator
position, or ++ or -- if it's only a change of one position.

Kohei

-- 
Kohei Yoshida, LibreOffice hacker, Calc
<kyoshida@novell.com>


Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.