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


Groups > comp.lang.c++ > #7757 > unrolled thread

STL algorithm for finding first non-matching value

Started byStephen Howe <stephen.howe@kantarmedia.com>
First post2011-07-12 13:03 -0700
Last post2011-07-20 13:15 -0700
Articles 11 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#7757 — STL algorithm for finding first non-matching value

FromStephen Howe <stephen.howe@kantarmedia.com>
Date2011-07-12 13:03 -0700
SubjectSTL algorithm for finding first non-matching value
Message-ID<c0c7e37f-3d7e-4ff0-8272-dae1b5b0ec87@b39g2000prd.googlegroups.com>
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

[toc] | [next] | [standalone]


#7758

FromKai-Uwe Bux <jkherciueh@gmx.net>
Date2011-07-12 22:19 +0200
Message-ID<ivia7q$mvm$1@hoshi.visyn.net>
In reply to#7757
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);

What about std::find_if()? You can use it with a functor that compares to 
Value.


Best,

Kai-Uwe Bux

[toc] | [prev] | [next] | [standalone]


#7759

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-07-12 21:27 +0100
Message-ID<lO-dnQGXL7uhM4HTnZ2dnUVZ8rCdnZ2d@giganews.com>
In reply to#7757
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));
}

/Leigh

[toc] | [prev] | [next] | [standalone]


#7761

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-07-12 21:50 +0100
Message-ID<U76dnQ481LANLoHTnZ2dnUVZ8mWdnZ2d@giganews.com>
In reply to#7759
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

[toc] | [prev] | [next] | [standalone]


#7763

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-07-12 22:02 +0100
Message-ID<coSdnRmTyq4fK4HTnZ2dnUVZ8tidnZ2d@giganews.com>
In reply to#7761
On 12/07/2011 21:50, Leigh Johnston wrote:
> 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.

Actually the performance of bind is probably acceptable in this case; I 
was actually thinking of the inefficiencies of std::tr1::function which 
typically involves a virtual function and perhaps an object allocation.

/Leigh

[toc] | [prev] | [next] | [standalone]


#7775

FromYakov Gerlovin <yakov.gerlovin@gmail.com>
Date2011-07-13 01:37 -0700
Message-ID<6d1af0c6-165c-4994-a085-8d08a45fd7fe@x12g2000yql.googlegroups.com>
In reply to#7763
> >> std::tr1::bind(std::not_equal_to<int>(), _1, 1));

Using std::bind1st (or bind2nd) instead of tr1::bind is more portable
solution:
    std::cout << *std::find_if(a.begin(), a.end(),
			std::bind1st( std::not_equal_to<int>(), 1));

(Make sure you don't dereference a.end() )

[toc] | [prev] | [next] | [standalone]


#7776

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-07-13 12:29 +0100
Message-ID<0uWdnTrPCIg_HIDTnZ2dnUVZ8l-dnZ2d@giganews.com>
In reply to#7775
On 13/07/2011 09:37, Yakov Gerlovin wrote:
>>>> std::tr1::bind(std::not_equal_to<int>(), _1, 1));
>
> Using std::bind1st (or bind2nd) instead of tr1::bind is more portable
> solution:
>      std::cout<<  *std::find_if(a.begin(), a.end(),
> 			std::bind1st( std::not_equal_to<int>(), 1));
>
> (Make sure you don't dereference a.end() )

bind1st and bind2nd are deprecated in C++0x; if you have TR1 available 
then you should use bind instead.

/Leigh

[toc] | [prev] | [next] | [standalone]


#7798

FromYakov Gerlovin <yakov.gerlovin@gmail.com>
Date2011-07-14 01:56 -0700
Message-ID<ec617872-431d-45a2-ad37-f6010dd38e70@g2g2000vbl.googlegroups.com>
In reply to#7776
> bind1st and bind2nd are deprecated in C++0x; if you have TR1 available
> then you should use bind instead.

Thank you Leigh, I wasn't aware bind1st and bind2nd are deprecated.
This group helps me a lot to keep up with changes.

Do you know since when g++ supports tr1::bind?
My version has tr1::array, but not tr1::bind
Thanks,
Yakov

[toc] | [prev] | [next] | [standalone]


#7801

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-07-14 13:37 +0100
Message-ID<vtCdnYQiueuAfoPTnZ2dnUVZ8oqdnZ2d@giganews.com>
In reply to#7798
On 14/07/2011 09:56, Yakov Gerlovin wrote:
>> bind1st and bind2nd are deprecated in C++0x; if you have TR1 available
>> then you should use bind instead.
>
> Thank you Leigh, I wasn't aware bind1st and bind2nd are deprecated.
> This group helps me a lot to keep up with changes.
>
> Do you know since when g++ supports tr1::bind?
> My version has tr1::array, but not tr1::bind

Are you sure you haven't got bind?  #include <functional>

> Thanks,
> Yakov
>

/Leigh

[toc] | [prev] | [next] | [standalone]


#7974

FromYakov Gerlovin <yakov.gerlovin@gmail.com>
Date2011-07-19 08:29 -0700
Message-ID<1af0be8e-903f-4edc-a05d-913e49e276a3@u26g2000vby.googlegroups.com>
In reply to#7801
> Are you sure you haven't got bind?  #include <functional>

My mistake, g++ 4.3.2 has bind
I just needed to include <functional> and to add using declaration for
_1
(using placeholders::_1)
I'm new to tr1, thank you Leigh for your help

[toc] | [prev] | [next] | [standalone]


#8009

FromJames Kanze <james.kanze@gmail.com>
Date2011-07-20 13:15 -0700
Message-ID<808d8a4b-d1b9-45f2-bd3b-9bf6313bf7fd@t5g2000yqj.googlegroups.com>
In reply to#7801
On Jul 14, 1:37 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 14/07/2011 09:56, Yakov Gerlovin wrote:

> >> bind1st and bind2nd are deprecated in C++0x; if you have TR1 available
> >> then you should use bind instead.

> > Thank you Leigh, I wasn't aware bind1st and bind2nd are deprecated.
> > This group helps me a lot to keep up with changes.

> > Do you know since when g++ supports tr1::bind?
> > My version has tr1::array, but not tr1::bind

> Are you sure you haven't got bind?  #include <functional>

I think in recent versions, it's std::bind, and not tr1:bind.
(For maximum portability, we use boost::bind:-).)

--
James Kanze

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web