# deleting an element from a vector

This topic is 4340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am trying to delete all elements from a vector except for the first one. However when I do that I get a segmentation fault. This is the code I am using to delete all the elements
for(std::vector<CDolphin *>::iterator itor = m_BaseVec.begin() + 1;
itor != m_BaseVec.end(); itor++)
{
(*itor)->DisposeResources();
delete (*itor);
m_BaseVec.erase(itor);

}


What am I doing wrong here ? Thanks

##### Share on other sites
At the end of the for loop, you erase the iterator, after which, you try to increment it (itor++). This is bad, and is almost definitely the cause of your errors.

If you just want a vector with the first element, use resize().

Edit: Also, its good practice to use the prefix increment operator (++itor) rather than the postfx (itor++). In the case of the latter, a copy of the element being incremented is made and returned, while the original element gets incremented (which is a little confusing, but think of it like this:
int x = 5;
int y = x++;
cout<<y; //y is assigned the copy of x that gets returned, so y = 5
cout<<x; //the original value of x is incremented after the copy is made, so x = 6). On the otherhand, when using the prefix ++itor, no copy is made. When dealing with larger classes or data types that may have overloaded increment operators, making a copy every increment can reduce performance. I doubt you'll see much improvement with ++itor vs. itor++, but I think its just a good practice to get in.

##### Share on other sites
vector::erase returns the iterator of the item after the one that has just been erased (or end() if no elements are left). So you can do this:

for(std::vector<CDolphin *>::iterator iter = m_BaseVec.begin() + 1; iter != m_BaseVec.end(); ){   (*iter)->DisposeResources();   delete (*iter);   iter = m_BaseVec.erase(iter);}

This is unrelated, but out of curiosity, is there a particular reason why you call DisposeResources() manually before delete rather than just calling DisposeResources from the class destructor?

##### Share on other sites
I'm asking this for him, because I'd like to know as well. Using resize, will the destructor get called for each element?

##### Share on other sites
Quote:
 Original post by rprellerI'm asking this for him, because I'd like to know as well. Using resize, will the destructor get called for each element?

No, so if you were to use it you would still need to delete all the elements. ApochPiQ's solution is a better one.

##### Share on other sites
You would be better off with std::for_each():
#include <vector>#include <algorithm>struct CDolphin {	void DisposeResources() {}};void MyDeleter( CDolphin * const p ) {	p->DisposeResources();	delete p;}int main() {	std::vector< CDolphin * > v;	v.push_back( new CDolphin() );	v.push_back( new CDolphin() );	v.push_back( new CDolphin() );	v.push_back( new CDolphin() );	std::for_each( v.begin() + 1, v.end(), &MyDeleter );	v.erase( v.begin() + 1, v.end() );}

[edit: Or, you might look into smart pointers/containers, such as boost::shared_ptr/boost::ptr_vector]

[edit2: Is there a reason for not calling DisposeResources() in the destructor of CDolphin?]

jfl.

[Edited by - jflanglois on July 6, 2006 1:07:14 AM]

##### Share on other sites
Actually, I think your problem lies in that you are deleting an element of the vector and continuing on to the next. But in order to get the next element of a vector, you must know where you came from, which you just deleted.

Try starting at vector.end()-1 and continuing down while the iterator != vector.begin().

Or just use resize().

• 16
• 13
• 23
• 10
• 19