Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL algorithm for finding first non-matching value

Reply
Thread Tools

STL algorithm for finding first non-matching value

 
 
Stephen Howe
Guest
Posts: n/a
 
      07-12-2011
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-12-2011
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

 
Reply With Quote
 
 
 
 
Yakov Gerlovin
Guest
Posts: n/a
 
      07-13-2011
> >> 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() )
 
Reply With Quote
 
Yakov Gerlovin
Guest
Posts: n/a
 
      07-14-2011
> 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

 
Reply With Quote
 
Yakov Gerlovin
Guest
Posts: n/a
 
      07-19-2011
> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2011
On Jul 14, 1:37 pm, Leigh Johnston <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL algorithm to compare if 2 vector<int> have same value? silverburgh.meryl@gmail.com C++ 4 10-29-2008 09:35 AM
STL algorithm to find max value in a set or map? cayblood C++ 5 11-03-2005 06:57 PM
STL - an algorithm for finding a collection within a collection? Dylan C++ 5 03-22-2005 01:31 AM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM
Q: (XSLT) finding the position of the first occurrence of an attribute value Jorn W Janneck XML 3 09-05-2003 04:09 AM



Advertisments