C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alex Vinokur
Guest





PostPosted: Tue Jul 11, 2006 9:10 am    Post subject: Iterators and pointers in vector<T>::insert() Reply with quote



vector<int> v (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
Back to top
Guest






PostPosted: Tue Jul 11, 2006 9:10 am    Post subject: Re: Iterators and pointers in vector<T>::insert() Reply with quote



Alex Vinokur wrote:
Quote:
vector<int> v (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
Back to top
Robbie Hatley
Guest





PostPosted: Tue Jul 11, 2006 9:10 am    Post subject: Re: Iterators and pointers in vector<T>::insert() Reply with quote



"Alex Vinokur" <alexvn (AT) users (DOT) sourceforge.net> wrote:

Quote:
vector<int> v (100);
v.insert( v.begin() + 5, 12 );
v.insert( &v[5], 17 ); // Is this legal?

No.

Quote:
// 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/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.