473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Vector erase dumps core when vector size is 2

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<intmyVect;
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;
}
Dec 5 '07 #1
5 2033
Anil wrote:
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<intmyVect;
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
Dec 5 '07 #2

"Anil" <an**********@gmail.comwrote in message
news:ca**********************************@w40g2000 hsb.googlegroups.com...
>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<intmyVect;
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
Dec 5 '07 #3
On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
main()
{
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()

--
Joel Yliluoma - http://iki.fi/bisqwit/
Dec 17 '07 #4
Joel Yliluoma wrote:
On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
>main()
{

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.
Dec 17 '07 #5
Anil wrote:
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<intmyVect;
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;
>
}

return 0;
}


--
Jim Langston
ta*******@rocketmail.com
Dec 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ganesh Gella | last post by:
Hi All, I am using g++ on Linux, and my code has lot of vectors each stores a particualr type of structure. (Structure internally had some vectors). When I am trying to push_back an element to...
3
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please...
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
8
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
18
by: John Salmon | last post by:
I was under the impression that the following was always valid: std::vector<Tv; .. T *p = &(v); But I was recently told that care was needed in case the vector was empty, i.e., when v.size()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.