By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Iterators and pointers in vector<T>::insert()

P: n/a
vector<intv (100);

v.insert( v.begin() + 5, 12 );

// ----------------------------------
// Is this legal?
v.insert( &v[5], 17 );
// P.S. Microsoft VC7 allows that
// ----------------------------------

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Alex Vinokur wrote:
vector<intv (100);

v.insert( v.begin() + 5, 12 );

// ----------------------------------
// Is this legal?
v.insert( &v[5], 17 );
Depends. On implementations where vector<T>::iterator is a T*,
yes. On others, no. Not portable, therefore.

HTH,
Michiel Salters

Jul 11 '06 #2

P: n/a
"Alex Vinokur" <al****@users.sourceforge.netwrote:
vector<intv (100);
v.insert( v.begin() + 5, 12 );
v.insert( &v[5], 17 ); // Is this legal?
No.
// P.S. Microsoft VC7 allows that
With some compilers, that will compile and run without
error, because std::vector<whatever>::iterator is often
implimented in terms of a "whatever*". But no, it's not
legal, nor advisible, nor portable.

Your "v.begin() + 5" is perfectly acceptable, though.
The standard specifically requires random-access iterators
(such as vector<whatever>::iterator ) to be capable of
having integers added and subtracted from them.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 11 '06 #3

P: n/a
Alex Vinokur wrote:
vector<intv (100);

v.insert( v.begin() + 5, 12 );

// ----------------------------------
// Is this legal?
v.insert( &v[5], 17 );
// P.S. Microsoft VC7 allows that
// ----------------------------------
I think it depends on the implementation of vector. In my case STL
implementation used by VC7 defined iterator as an inner class of vector
and it could implicitly convert a "pointer" (&v[5]) to an iterator via
an overloaded constructor. However, on my Linux box (gcc 3.2.2) vector
had typedefed iterator to __gnu_cxx::__normal_iterator which does not
implicitly convert a pointer to an iterator and hence the above code
fails to compile.

Thanks and regards
SJ

Jul 11 '06 #4

P: n/a
In message <11*********************@s13g2000cwa.googlegroups. com>, Alex
Vinokur <al****@users.sourceforge.netwrites
>vector<intv (100);

v.insert( v.begin() + 5, 12 );

// ----------------------------------
// Is this legal?
v.insert( &v[5], 17 );
No. &v[5] is a pointer, not an iterator, so it's not portable.
>// P.S. Microsoft VC7 allows that
There's nothing in the standard to stop the library author implementing
vector<int>::iterator using int*. There's nothing to say that they
must. You can't rely on it.

--
Richard Herring
Jul 11 '06 #5

P: n/a
Alex Vinokur wrote:
v.insert( &v[5], 17 );
// P.S. Microsoft VC7 allows that
2005 (7.1) I believe will NOT in DEBUG mode. It specifically
uses an iterator class and puts some extra checking (for example
we get faults randomly incremting the iterator way past the end
of the vector).
Jul 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.