Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11021
| From | Edek <edek.pienkowski@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Strange remove_if behaviour |
| Date | 2011-10-20 18:02 +0200 |
| Organization | ATMAN - ATM S.A. |
| Message-ID | <j7pgn1$7bd$1@node2.news.atman.pl> (permalink) |
| References | <b4e9c98d-5021-4ee8-bfb1-dedab2151118@v7g2000yqf.googlegroups.com> <87k480rmvo.fsf@dpt-info.u-strasbg.fr> <ca980f26-4081-49f7-bdae-3ac06d7437a1@i19g2000yqa.googlegroups.com> |
On 10/20/2011 02:17 PM, hugo_ribeira@hotmail.com wrote:
> On 20 Out, 11:14, Alain Ketterlin<al...@dpt-info.u-strasbg.fr> wrote:
>> "hugo_ribe...@hotmail.com"<hugo_ribe...@hotmail.com> writes:
>>> int x[] = {1,2,1,4,3,2,1,4,5,6,7,8,1,3,1,3};
>>> vector<int> y(x, x + sizeof(x)/sizeof(int));
>>
>>> class equals
>>> {
>>> int _test_case;
>>
>>> public:
>>> equals(int test_case): _test_case(test_case) {}
>>> bool operator()(int var)
>>> {
>>> return _test_case == var;
>>> }
>>> };
>>
>>> int main()
>>> {
>>> for(auto i = y.begin(); i< y.end(); ++i)
>>> cout<< *i;
>>> cout<< endl<< endl;
>>> remove_if(y.begin(), y.end(), equals(1));
>>> for(auto i = y.begin(); i< y.end(); ++i)
>>> cout<< *i;
>>> return EXIT_SUCCESS;
>>> }
>>> "
>>
>>> It's suposed to remove all the 1's from y, but it isn't. This is the
>>> output:
>>
>>> 1214321456781313
>>
>>> 2432456783381313
>>
>>> Those 1's were not suposed to be there, or were they?
>>
>> Yes, this is documentted behavior: remove doesn't remove, it moves them
>> to the back and returns an iterator to the new "end". You need an
>> additional call to y.erase().
>>
>> -- Alain.
>
> But it removed the first 3 ocurrences of 1, it just left the last two.
>
> Besides I'm printing the entire vector so if they were moved to the
> back should it be in the output?
Nothing is actually moved to the back, only moved to front, but
that is an implementation detail and may vary.
The front is stripped if deleted. It is a common misconception,
sometimes people do not read the details in remove_if's documentation,
including myself. Read it carefully again.
One alternative may be remove_copy_if - for remove_if you either use
the returned iterator, or do an extra erase also using this iterator
and you get a good result in the container instead of iterator pair
[start,end)
Edek
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Strange remove_if behaviour "hugo_ribeira@hotmail.com" <hugo_ribeira@hotmail.com> - 2011-10-20 02:58 -0700
Re: Strange remove_if behaviour Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-10-20 12:14 +0200
Re: Strange remove_if behaviour "hugo_ribeira@hotmail.com" <hugo_ribeira@hotmail.com> - 2011-10-20 05:17 -0700
Re: Strange remove_if behaviour Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-20 08:29 -0400
Re: Strange remove_if behaviour Edek <edek.pienkowski@gmail.com> - 2011-10-20 18:02 +0200
Re: Strange remove_if behaviour Ben Cottrell <email@address.invalid> - 2011-10-20 17:25 +0100
Re: Strange remove_if behaviour Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-21 07:58 +0000
Re: Strange remove_if behaviour Stephen Howe <stephen.howe@kantarmedia.com> - 2011-10-20 14:42 -0700
Re: Strange remove_if behaviour Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-10-21 10:20 +0200
Re: Strange remove_if behaviour "hugo_ribeira@hotmail.com" <hugo_ribeira@hotmail.com> - 2011-10-22 15:41 -0700
csiph-web