Vector erase dumps core when vector size is 2

A

Anil

I am facing problem while erasing an elemet from stl vector when its
size is 2. It works fine when SIZE > 2.
Can anybody help me in this?? Following is the sample code which i
tried.

#include <iostream>
#include <vector>

using namespace std;
#define SIZE 2
main()
{
vector<int> myVect;
std::vector<int>::iterator iter;

for(int i =0; i<SIZE; i++)
myVect.push_back(i);

cout<< myVect.size() << "\n";
for( iter = myVect.begin(); iter != myVect.end(); iter++)
cout<< *iter << " ";

cout<<"\n";

for( iter = myVect.begin(); iter != myVect.end(); iter++)
{
cout<< *iter << "\n";
if( *iter == 1)
myVect.erase(iter);

}

return 0;
}
 
M

Mark P

Anil said:
I am facing problem while erasing an elemet from stl vector when its
size is 2. It works fine when SIZE > 2.
Can anybody help me in this?? Following is the sample code which i
tried.

#include <iostream>
#include <vector>

using namespace std;
#define SIZE 2
main()
{
vector<int> myVect;
std::vector<int>::iterator iter;

for(int i =0; i<SIZE; i++)
myVect.push_back(i);

cout<< myVect.size() << "\n";
for( iter = myVect.begin(); iter != myVect.end(); iter++)
cout<< *iter << " ";

cout<<"\n";

for( iter = myVect.begin(); iter != myVect.end(); iter++)
{
cout<< *iter << "\n";
if( *iter == 1)
myVect.erase(iter);

}

return 0;
}

Calling erase invalidates any iterators pointing at or after the erased
element. The canonical way to do this is, rather than ++iter in the loop:

iter = myVect.erase(iter);

Read footnote 5 here: http://www.sgi.com/tech/stl/Vector.html#5
Read about erase here: http://www.sgi.com/tech/stl/Sequence.html

-Mark
 
H

Howard

Anil said:
I am facing problem while erasing an elemet from stl vector when its
size is 2. It works fine when SIZE > 2.
Can anybody help me in this?? Following is the sample code which i
tried.

#include <iostream>
#include <vector>

using namespace std;
#define SIZE 2
main()
{
vector<int> myVect;
std::vector<int>::iterator iter;

for(int i =0; i<SIZE; i++)
myVect.push_back(i);

cout<< myVect.size() << "\n";
for( iter = myVect.begin(); iter != myVect.end(); iter++)
cout<< *iter << " ";

cout<<"\n";

for( iter = myVect.begin(); iter != myVect.end(); iter++)
{
cout<< *iter << "\n";
if( *iter == 1)
myVect.erase(iter);

}

return 0;
}

I think that calling erase above causes iter to become invalidated, so it's
illegal to call iter++ on it afterwards, regardless of whether it seems to
work on some vectors. The erase functions returns an iterator to the next
item, so you can set iter to that when calling erase, and only incrementing
when not calling erase. I believe there's also a std algorithm for this
(remove_if or something like that, I forget).

-Howard
 
J

Joel Yliluoma


Is there some popular course that mistakenly teaches
declaring the main() function like that? Such line
seems common in questions lately in this group.

The proper minimal way to declare main(), is of course:

int main()
 
R

red floyd

Joel said:
Is there some popular course that mistakenly teaches
declaring the main() function like that? Such line
seems common in questions lately in this group.

Welcome to India.
 
J

Jim Langston

Anil said:
I am facing problem while erasing an elemet from stl vector when its
size is 2. It works fine when SIZE > 2.
Can anybody help me in this?? Following is the sample code which i
tried.

#include <iostream>
#include <vector>

using namespace std;
#define SIZE 2
main()
{
vector<int> myVect;
std::vector<int>::iterator iter;

for(int i =0; i<SIZE; i++)
myVect.push_back(i);

cout<< myVect.size() << "\n";
for( iter = myVect.begin(); iter != myVect.end(); iter++)
cout<< *iter << " ";

cout<<"\n";
for( iter = myVect.begin(); iter != myVect.end(); )
{
cout<< *iter << "\n";
if( *iter == 1)
iter = myVect.erase(iter);
else
++iter;
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top