STL algorithm for finding first non-matching value

S

Stephen Howe

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
 
K

Kai-Uwe Bux

Stephen said:
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
 
Y

Yakov Gerlovin

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() )
 
Y

Yakov Gerlovin

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
 
Y

Yakov Gerlovin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top