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

A

Alex Vinokur

vector<int> v (100);

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

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

Michiel.Salters

Alex said:
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
 
R

Robbie Hatley

Alex Vinokur said:
vector<int> v (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/
 
S

sonison.james

Alex said:
vector<int> v (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
 
R

Richard Herring

Alex said:
vector<int> v (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.
 
R

Ron Natalie

Alex said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top