Topic: STL: Iterator to a given vector index?
Author: Uwe Tantz <tau04037@rphc1.physik.uni-regensburg.de>
Date: 1996/11/04 Raw View
I often use vector-indices where, perhaps, iterators might be more
adequate. So I have sometimes the problem to convert an index to the
corresponding iterator.
Is this peace of code conforming to the standard?
...
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
...
int index=2;
...
vector<int>::iterator it=&v[index];
^^^^^^^^^ conforming?
...
I think it can only be correct, if vector-iterators are implemented as
ordinary C-pointers. My implementation is done that way. On the other
hand, my book about STL (Josuttis) says they are [just] USUALLY
implemented as pointers.
however, if the code above is incorrect, why is there no vector-method
returning an iterator?
Thanks
Uwe Tantz
PS:
BTW is
vector<int>::iterator it=v.begin()+index;
conforming?
uwe.tantz@physik.uni-regensburg.de
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: ken@digitas.org (Ken Shan)
Date: 1996/11/04 Raw View
Uwe Tantz (tau04037@rphc1.physik.uni-regensburg.de) wrote:
> vector<int>::iterator it=&v[index];
> ^^^^^^^^^ conforming?
This is not conforming, but
int *pi = &v[index];
or just
int &ri = v[index];
is, since operator[] returns int &. What you want is probably
> vector<int>::iterator it=v.begin()+index;
which is conforming.
--
blue | Ken; Shan, Chung-chieh; Sian7, Tiong1-kiat8; ccshan@fas.harvard.edu.
() | Your code today becomes the mind tomorrow: Your plan its means,
/\ | your dream its ends, your ideal its elegance. Hack on.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1996/11/04 Raw View
Uwe Tantz <tau04037@rphc1.physik.uni-regensburg.de> writes:
> PS:
> BTW is
> vector<int>::iterator it=v.begin()+index;
> conforming?
Yes. Vector<T>::iterator is a random access iterator. It may or may
not be implemented as a T*, but it is guaranteed to be a random access
iterator. This means that is must support all of the operations in
the random access iterator requirements table (table 86, in clause
24.1.5). One of those operations is adding a constant to an iterator.
This is the best way to find an iterator that points to the n'th
element of a 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]