472,959 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,959 software developers and data experts.

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 1416
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
6
by: hannibal200480 | last post by:
Hi everyone, Here about what it is. vector<vector<Point3D> > pVector0; pVector0 is a vector of vector of 3D points. pVector0 is a vector of 3D points.
1
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
0
by: subramanian100in | last post by:
Suppose I have vector<intc; for (int i = 0; i < 10; ++i) c.push_back(i); vector<int>::iterator it = find(c.begin(), c.end(), 5); If I do, c.insert(c.begin(), 10);
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
2
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type...
12
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.