 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alex Vinokur Guest
|
Posted: Tue Jul 11, 2006 9:10 am Post subject: Iterators and pointers in vector<T>::insert() |
|
|
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
|
Posted: Tue Jul 11, 2006 9:10 am Post subject: Re: Iterators and pointers in vector<T>::insert() |
|
|
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
|
Posted: Tue Jul 11, 2006 9:10 am Post subject: Re: Iterators and pointers in vector<T>::insert() |
|
|
"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 |
|
 |
|
|
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
|
|