Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11007

Re: Strange remove_if behaviour

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Strange remove_if behaviour
Date 2011-10-20 08:29 -0400
Organization A noiseless patient Spider
Message-ID <j7p479$lt3$2@dont-email.me> (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>

Show all headers | View raw


On 10/20/2011 8:17 AM, 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?

Can you read?  Then *read* the documentation.  You need to use the 
return value of 'remove_if'.  Instead of printing to '.end()', print 
only until the return value:

     vector<int>::iterator newend = remove_if(...

     for (auto i = y.begin(); i != newend; ++i) ...

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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