 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
david wolf Guest
|
Posted: Tue Apr 19, 2005 4:02 pm Post subject: delete elements in vector |
|
|
I want to delete all even numbers in a vector, I am not sure if there's
any better way to do it. Following program is how I did it.
Look at the part of code beginning from
comments: //delete all even numbers.
My questions is actullay I have to put pos++ in the body of the
loop(also in if, else, statement). Can I somehow put it in
for(...;...;...).
Or can I use index instead of iterator in order to delete elements from
vector?
I just wish to know what's the most elegant way to do it.
Thanks
#include <iostream>
#include <vector>
main(){
int ar[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> tmpint;
//populate vector
for(int i=0; i<10; i++){
tmpint.push_back(ar[i]);
}
//delete all even numbers
vector
for(pos=tmpint.begin(); pos != tmpint.end(); ){
if (*pos % 2 == 0)
tmpint.erase(pos);
else
pos++;
}
//display all elements in the vector
for(pos=tmpint.begin(); pos != tmpint.end();pos++ ){
cout<<*pos<
}
}
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Apr 19, 2005 4:28 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | I want to delete all even numbers in a vector, I am not sure if there's
any better way to do it. Following program is how I did it.
Look at the part of code beginning from
comments: //delete all even numbers.
My questions is actullay I have to put pos++ in the body of the
loop(also in if, else, statement). Can I somehow put it in
for(...;...;...).
Or can I use index instead of iterator in order to delete elements from
vector?
I just wish to know what's the most elegant way to do it.
|
Elegance is in the eye of the beholder. You can try doing it in one line
(well, not exactly, you'll need to define your predicate), but whether
it's "elegant" or not is not my place to say.
| Quote: |
Thanks
#include <iostream
#include
main(){
|
int main(){
| Quote: |
int ar[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector
|
std::vector<int> tmpint;
| Quote: |
//populate vector
for(int i=0; i<10; i++){
tmpint.push_back(ar[i]);
}
|
Did you know you can initialise 'tmpint' from 'ar' in the same statement
where you declared it?
std::vector
?? No need for the loop at all...
| Quote: |
//delete all even numbers
vector<int>::iterator pos;
|
std::vector<int>::iterator pos;
| Quote: | for(pos=tmpint.begin(); pos != tmpint.end(); ){
if (*pos % 2 == 0)
tmpint.erase(pos);
else
pos++;
}
//display all elements in the vector
for(pos=tmpint.begin(); pos != tmpint.end();pos++ ){
cout<<*pos<
|
std::cout << *pos << std::endl;
If you use 'std::remove_if' and a custom predicate, you can do
#include
#include <vector>
#include <algorithm>
using namespace std;
// your custom predicate
template<class T> struct is_even {
bool operator()(T t) const {
return t % 2 == 0;
}
};
int main(){
int ar[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> tmpint(ar, ar+10);
// this is essentially your one-liner.
tmpint.erase(
remove_if(tmpint.begin(),
tmpint.end(),
is_even<int>()),
tmpint.end());
//display all elements in the vector
for (size_t i = 0; i < tmpint.size(); i++)
cout << tmpint[i] << ' ';
cout << endl;
}
V
|
|
| Back to top |
|
 |
Mark Stijnman Guest
|
Posted: Wed Apr 20, 2005 1:26 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | I want to delete all even numbers in a vector, I am not sure if
there's
any better way to do it. Following program is how I did it.
Look at the part of code beginning from
comments: //delete all even numbers.
My questions is actullay I have to put pos++ in the body of the
loop(also in if, else, statement). Can I somehow put it in
for(...;...;...).
|
You probably could, but I don't think it's worth it.
| Quote: |
Or can I use index instead of iterator in order to delete elements
from
vector?
I just wish to know what's the most elegant way to do it.
Thanks
#include <iostream
#include
main(){
int ar[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector
//populate vector
for(int i=0; i<10; i++){
tmpint.push_back(ar[i]);
}
//delete all even numbers
vector
for(pos=tmpint.begin(); pos != tmpint.end(); ){
if (*pos % 2 == 0)
tmpint.erase(pos);
else
pos++;
}
//display all elements in the vector
for(pos=tmpint.begin(); pos != tmpint.end();pos++ ){
cout<<*pos<
}
}
|
I'm not convinced this piece of code even does what it is supposed to
do - AFAIK the iterator pos is not guaranteed to be valid after calling
erase. I think you should use
pos = tmpint.erase(pos);
instead. This will make pos point to the next, non-erased element after
pos, or end() if there aren't any.
As far as elegance goes, the method erase works in linear time on
vectors: to erase an element, it simply moves all elements behind it
left one place (filing up the erased spot). This will create a lot of
unnecessary copying if you call erase lots of time in succession on the
same vector. So this is not very elegant I'm afraid. It is of course
possible to examine/erase/move every element in the vector only once,
if you think about it for a bit. It is possible that the remove_if that
Victor Bazarov suggested is implemented like that (I haven't checked,
but it seems likely) so I second his suggestion that you give that a
try.
grtz Mark
|
|
| Back to top |
|
 |
david wolf Guest
|
Posted: Wed Apr 20, 2005 7:44 pm Post subject: Re: delete elements in vector |
|
|
I do not agree with grtz Mark. I tried my program, it works fine. I
means it is not necessary to use
pos = tmpint.erase(pos);
simply doing
tmpint.erase(pos);
will work. Any one giving me some opnions?
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Apr 20, 2005 7:54 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | I do not agree with grtz Mark. I tried my program, it works fine. I
means it is not necessary to use
pos = tmpint.erase(pos);
simply doing
tmpint.erase(pos);
will work. Any one giving me some opnions?
|
Mark is right. Once you erase the 'pos' iterator, the next loop tries
to use it by dereferencing it. You simply get awfully lucky that it
stays the same on your system. 'pos' is invalidated by the call to
erase(), and dereferencing it invokes undefined behaviour. You ought
to use pos = tmpint.erase(pos);
V
|
|
| Back to top |
|
 |
david wolf Guest
|
Posted: Thu May 05, 2005 5:42 pm Post subject: Re: delete elements in vector |
|
|
I mean on Linux (red hat) system, I am using gcc. Whenever I did
tmpint.erase(pos);
pos automatically points to the element after the deleted one.
Can you tell me a case/os that this will not work?
------------------------------- Victor said -------------
'pos' is invalidated by the call to
erase(), and dereferencing it invokes undefined behaviour
------------------------------------------------------------------------------
Also, victor said above things. Could victor tell me where is it
specified to work this way?
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu May 05, 2005 6:07 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | I mean on Linux (red hat) system, I am using gcc. Whenever I did
tmpint.erase(pos);
pos automatically points to the element after the deleted one.
Can you tell me a case/os that this will not work?
|
It will not work if the container you're using is not 'vector'.
| Quote: | ------------------------------- Victor said -------------
'pos' is invalidated by the call to
erase(), and dereferencing it invokes undefined behaviour
------------------------------------------------------------------------------
Also, victor said above things. Could victor tell me where is it
specified to work this way?
|
I take it back. It is not invalidated, dereferencing it may not cause
undefined behaviour. Undefined behaviour only occurs if 'pos' was the
last element in the vector. However, for other containers it is true,
and if the element you're erasing is the last one, it is true. So, in
a _general_case_ it is true, then.
V
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Thu May 05, 2005 6:43 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | I do not agree with grtz Mark. I tried my program, it works fine. I
means it is not necessary to use
pos = tmpint.erase(pos);
|
Please quote enough of the previous message to provide context for your
replies. To do so using the Google interface, click on "show options"
and use the Reply shown in the expanded header.
Brian
|
|
| Back to top |
|
 |
david wolf Guest
|
Posted: Wed May 11, 2005 7:46 pm Post subject: Re: delete elements in vector |
|
|
Victor Bazarov wrote:
| Quote: | david wolf wrote:
I mean on Linux (red hat) system, I am using gcc. Whenever I did
tmpint.erase(pos);
pos automatically points to the element after the deleted one.
Can you tell me a case/os that this will not work?
It will not work if the container you're using is not 'vector'.
------------------------------- Victor said -------------
'pos' is invalidated by the call to
erase(), and dereferencing it invokes undefined behaviour
------------------------------------------------------------------------------
Also, victor said above things. Could victor tell me where is it
specified to work this way?
I take it back. It is not invalidated, dereferencing it may not
cause
undefined behaviour. Undefined behaviour only occurs if 'pos' was
the
last element in the vector. However, for other containers it is
true,
and if the element you're erasing is the last one, it is true. So,
in
a _general_case_ it is true, then.
V
|
I tried and found out that, even 'pos' was the last element in the
vector. If
we invoke:
tmpint.erase(pos);
pos is still defined after this erase method. After execute erase(),
pos will be point to the end of the container which means
pos=tmpint.end(). So at least for vector, we do not have use pos =
tmpint.erase(pos), I think.
Can you help confirm this?
David
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed May 11, 2005 8:04 pm Post subject: Re: delete elements in vector |
|
|
david wolf wrote:
| Quote: | [...]
I tried and found out that, even 'pos' was the last element in the
vector. If
we invoke:
tmpint.erase(pos);
pos is still defined after this erase method.
|
And how *did* you find that out?
| Quote: | After execute erase(),
pos will be point to the end of the container which means
pos=tmpint.end(). So at least for vector, we do not have use pos =
tmpint.erase(pos), I think.
Can you help confirm this?
|
No. If 'pos == tmpint.end()' yields 'true', then you cannot use 'pos'
to do anything except compare its value to the result of calling 'end()'
or anything else you usually do with the result of calling 'end()'. The
Standard gives no guarantees that "one-past-the-end" iterator would be
dereferenceable. It therefore is not necessarily valid. There are no
special provisions in the Standard regarding 'vector' and its iterators
in that respect.
V
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|