469,291 Members | 1,847 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

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 3978
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.