STL algorithm for finding first non-matching value

Discussion in 'C++' started by Stephen Howe, Jul 12, 2011.

  1. Stephen Howe

    Stephen Howe Guest

    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
     
    Stephen Howe, Jul 12, 2011
    #1
    1. Advertising

  2. Stephen Howe

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 12, 2011
    #2
    1. Advertising

  3. > >> 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() )
     
    Yakov Gerlovin, Jul 13, 2011
    #3
  4. > 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
     
    Yakov Gerlovin, Jul 14, 2011
    #4
  5. > 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
     
    Yakov Gerlovin, Jul 19, 2011
    #5
  6. Stephen Howe

    James Kanze Guest

    On Jul 14, 1:37 pm, Leigh Johnston <> 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
     
    James Kanze, Jul 20, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dylan
    Replies:
    5
    Views:
    432
    Daniel T.
    Mar 22, 2005
  2. cayblood
    Replies:
    5
    Views:
    6,246
    Calum Grant
    Nov 3, 2005
  3. Dilip
    Replies:
    1
    Views:
    518
    Dilip
    Oct 10, 2006
  4. Replies:
    4
    Views:
    921
    James Kanze
    Oct 29, 2008
  5. Robb Meade
    Replies:
    4
    Views:
    128
    Evertjan.
    Jun 29, 2003
Loading...

Share This Page