|
-
October 26th, 2010, 02:53 AM
#1
Erase with iterator
Hi,
I heard that erasing elements using iterator is dangerous.
But I tried the below program in gcc and it is running fine.
Please explain how it is dangerous
Code:
#include<vector>
#include<iterator>
#include<iostream>
using namespace std;
int main()
{
vector<int>vec;
vec.push_back(123);
vec.push_back(12);
vec.push_back(23);
vec.push_back(123);
vec.push_back(1123);
vec.push_back(111);
vec.push_back(2223);
copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
cout<<endl;
vector<int>::iterator itr;
for(itr=vec.begin();itr!=vec.end();itr++)
{
if(*itr == 123)
vec.erase(itr);
}
copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
cout<<endl;
}
output is:
123---12---23---123---1123---111---2223---
12---23---1123---111---2223---
-
October 26th, 2010, 03:04 AM
#2
Re: Erase with iterator
your humble savant
-
October 26th, 2010, 03:08 AM
#3
Re: Erase with iterator
 Originally Posted by Rajesh1978
Please explain how it is dangerous
The problem is that the call to erase invalidates the iterator (and iterators pointing to subsequent elements). You could avoid this by writing:
Code:
for (itr = vec.begin(); itr != vec.end();)
{
if (*itr == 123)
{
itr = vec.erase(itr);
}
else
{
++itr;
}
}
But it would be simpler and safer to write:
Code:
vec.erase(remove(vec.begin(), vec.end(), 123), vec.end());
By the way, you forgot to #include <algorithm>.
-
October 26th, 2010, 03:17 AM
#4
Re: Erase with iterator
Thanks
I understood now.
But I surprised how my code ran without any problem.
-
October 26th, 2010, 03:26 AM
#5
Re: Erase with iterator
Code:
#include<vector>
#include<iterator>
#include<iostream>
using namespace std;
int main()
{
vector<int>vec;
vec.push_back(123);
vec.push_back(12);
vec.push_back(23);
vec.push_back(123);
vec.push_back(123);
vec.push_back(1123);
vec.push_back(111);
vec.push_back(2223);
copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
cout<<endl;
vector<int>::iterator itr;
for(itr=vec.begin();itr!=vec.end();itr++)
{
if(*itr == 123)
vec.erase(itr);
}
copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
cout<<endl;
}
output is:
Code:
123---12---23---123---123---1123---111---2223---
12---23---123---1123---111---2223---
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 26th, 2010, 03:31 AM
#6
Re: Erase with iterator
Thanks a lot
You people gave a very vivid explanation and made me understood.
Regards
-
October 26th, 2010, 03:35 AM
#7
Re: Erase with iterator
 Originally Posted by Rajesh1978
Hi,
I heard that erasing elements using iterator is dangerous.
But I tried the below program in gcc and it is running fine.
Please explain how it is dangerous
When you make a mistake in a C++ program, there is no guarantee how your program will run. It may "work", it may crash, it may work sometimes and crash another time, it may work differently if compiled with a different compiler or different options, etc.
So writing code, and then seeing that it runs does not mean that the code is OK. If you're told why something in C++ is undefined, don't try and write code to see what happens -- you don't know what will happen. Unlike other computer languages, C++ doesn't automatically tell you that you did something wrong when you run a program.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 26th, 2010 at 03:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|