471,571 Members | 1,142 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Is this a valid used of STL vector::erase()

Hi all,

I recently came across the following code:

// some data
struct Data { // ... some data };

// a list to hold the data
vector<Data> dataList;

// a function to erase an element inside the datalist
void destroyDataAtIndex(int index)
{
dataList.erase( &dataList[index] ); //(a)
}

Is line (a) valid? If so, how does that work since the function
declaration of erase is erase(iterator iter)? Does it automatically
converts the address of the element to an iterator? or it makes the
assumption that an iterator is a typedef of pointer?

Also, I created a toy program with the above code snipplet, from the
test runs, some times the program will crashed. Is it because of the
way we used erase in (a)?

Thanks,
Kaede
Jul 22 '05 #1
1 4024
On 22 Nov 2003 21:16:45 -0800, ka************@hotmail.com (kaede) wrote:
Hi all,

I recently came across the following code:

// some data
struct Data { // ... some data };

// a list to hold the data
vector<Data> dataList;

// a function to erase an element inside the datalist
void destroyDataAtIndex(int index)
{
dataList.erase( &dataList[index] ); //(a)
}

Is line (a) valid? If so, how does that work since the function
declaration of erase is erase(iterator iter)? Does it automatically
converts the address of the element to an iterator? or it makes the
assumption that an iterator is a typedef of pointer?


No, it's not valid. However, it might work on some compilers that use
pointers as iterators for vectors. It will fail on others.

--
Be seeing you.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by BCC | last post: by
10 posts views Thread by Alex Vinokur | last post: by
9 posts views Thread by Amadeus W. M. | last post: by
3 posts views Thread by ma740988 | last post: by
3 posts views Thread by subramanian100in | last post: by
mickey0
1 post views Thread by mickey0 | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

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.