468,456 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

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

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
5 1206
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
"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
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
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
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.

Similar topics

7 posts views Thread by Dave | last post: by
14 posts views Thread by Dave | last post: by
6 posts views Thread by hannibal200480 | last post: by
7 posts views Thread by Renzr | last post: by
reply views Thread by subramanian100in | last post: by
2 posts views Thread by subramanian100in | last post: by
12 posts views Thread by eiji.anonremail | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.