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


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

Re: STL algorithm for finding first non-matching value

Date 2011-07-12 21:50 +0100
From Leigh Johnston <leigh@i42.co.uk>
Newsgroups comp.lang.c++
Subject Re: STL algorithm for finding first non-matching value
References <c0c7e37f-3d7e-4ff0-8272-dae1b5b0ec87@b39g2000prd.googlegroups.com> <lO-dnQGXL7uhM4HTnZ2dnUVZ8rCdnZ2d@giganews.com>
Message-ID <U76dnQ481LANLoHTnZ2dnUVZ8mWdnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 12/07/2011 21:27, Leigh Johnston wrote:
> On 12/07/2011 21:03, Stephen Howe wrote:
>> Hi
>>
>> Maybe I overlooking the obvious, but given that == operator or an
>> equality predicate exists,
>> is there an algorithm that find that first value that does not match
>> (or end if everything matches)?
>> This would be linear (rather that use lower_bound/upper_bound if
>> ordered)
>>
>> Essentially
>>
>> it = some_algorithm(ForwardIteratorStart, ForwardIteratorEnd, Value,
>> Predicate);
>>
>> Thanks
>>
>> Stephen Howe
>
> std::not1 or std::not2 perhaps?
>
> int main()
> {
> std::tr1::array<int, 5> a = { 1, 1, 1, 42 };
> std::cout << *std::find_if(a.begin(), a.end(),
> std::tr1::bind(std::not2(std::equal_to<int>()), _1, 1));
> }
>
> Perhaps using the std::not_equal_to is the more obvious solution:
>
> int main()
> {
> std::tr1::array<int, 5> a = { 1, 1, 1, 42 };
> std::cout << *std::find_if(a.begin(), a.end(),
> std::tr1::bind(std::not_equal_to<int>(), _1, 1));
> }

If you are worried about efficiency then writing your own predicate 
functor rather than using bind might be the optimal choice.

HTH.

/Leigh

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


Thread

STL algorithm for finding first non-matching value Stephen Howe <stephen.howe@kantarmedia.com> - 2011-07-12 13:03 -0700
  Re: STL algorithm for finding first non-matching value Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-12 22:19 +0200
  Re: STL algorithm for finding first non-matching value Leigh Johnston <leigh@i42.co.uk> - 2011-07-12 21:27 +0100
    Re: STL algorithm for finding first non-matching value Leigh Johnston <leigh@i42.co.uk> - 2011-07-12 21:50 +0100
      Re: STL algorithm for finding first non-matching value Leigh Johnston <leigh@i42.co.uk> - 2011-07-12 22:02 +0100
        Re: STL algorithm for finding first non-matching value Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-13 01:37 -0700
          Re: STL algorithm for finding first non-matching value Leigh Johnston <leigh@i42.co.uk> - 2011-07-13 12:29 +0100
            Re: STL algorithm for finding first non-matching value Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-14 01:56 -0700
              Re: STL algorithm for finding first non-matching value Leigh Johnston <leigh@i42.co.uk> - 2011-07-14 13:37 +0100
                Re: STL algorithm for finding first non-matching value Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-19 08:29 -0700
                Re: STL algorithm for finding first non-matching value James Kanze <james.kanze@gmail.com> - 2011-07-20 13:15 -0700

csiph-web