using std::find on a std::vector<double>

Discussion in 'C++' started by Anonymous, Oct 16, 2007.

  1. Anonymous

    Anonymous Guest

    I ahve a vector of doubles taht I need to extract values from. I was
    just about to use the STL find() algo, but I have a couple of questions:

    first: can you specify the tolerance threshold to match on doubles?
    second: if yes, how may this be done (i.e. how many one specify the
    tolerance threshold for comparing doubles?)
    Anonymous, Oct 16, 2007
    #1
    1. Advertising

  2. Anonymous

    Kai-Uwe Bux Guest

    Anonymous wrote:

    > I ahve a vector of doubles taht I need to extract values from. I was
    > just about to use the STL find() algo, but I have a couple of questions:
    >
    > first: can you specify the tolerance threshold to match on doubles?
    > second: if yes, how may this be done (i.e. how many one specify the
    > tolerance threshold for comparing doubles?)


    Note that find() has two signatures: one that takes a value to be found and
    one that takes a unary predicate instead. Use the later and provide a
    functor:

    class is_approximately {

    double x;

    public:

    is_approximately ( double what )
    : x ( what )
    {}

    bool operator() ( double y ) const {
    // return true if y is near x.
    }

    };

    Now you can do:

    find( whatever.begin(), whatever.end(), is_approximately( 0.5 ) );



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 16, 2007
    #2
    1. Advertising

  3. Anonymous

    red floyd Guest

    Kai-Uwe Bux wrote:
    > Anonymous wrote:
    >
    >> I ahve a vector of doubles taht I need to extract values from. I was
    >> just about to use the STL find() algo, but I have a couple of questions:
    >>
    >> first: can you specify the tolerance threshold to match on doubles?
    >> second: if yes, how may this be done (i.e. how many one specify the
    >> tolerance threshold for comparing doubles?)

    >
    > Note that find() has two signatures: one that takes a value to be found and
    > one that takes a unary predicate instead. Use the later and provide a
    > functor:


    Isn't the second form find_if(), not find()?
    >
    > class is_approximately {
    >
    > double x;
    >
    > public:
    >
    > is_approximately ( double what )
    > : x ( what )
    > {}
    >
    > bool operator() ( double y ) const {
    > // return true if y is near x.
    > }
    >
    > };
    >
    > Now you can do:
    >
    > find( whatever.begin(), whatever.end(), is_approximately( 0.5 ) );
    >
    >
    >
    > Best
    >
    > Kai-Uwe Bux
    red floyd, Oct 16, 2007
    #3
  4. Anonymous

    Kai-Uwe Bux Guest

    red floyd wrote:

    > Kai-Uwe Bux wrote:
    >> Anonymous wrote:
    >>
    >>> I ahve a vector of doubles taht I need to extract values from. I was
    >>> just about to use the STL find() algo, but I have a couple of questions:
    >>>
    >>> first: can you specify the tolerance threshold to match on doubles?
    >>> second: if yes, how may this be done (i.e. how many one specify the
    >>> tolerance threshold for comparing doubles?)

    >>
    >> Note that find() has two signatures: one that takes a value to be found
    >> and one that takes a unary predicate instead. Use the later and provide a
    >> functor:

    >
    > Isn't the second form find_if(), not find()?


    My bad. I had the page from the standard on my screen right in front of me.
    Clearly, I just can't read :-(


    >>
    >> class is_approximately {
    >>
    >> double x;
    >>
    >> public:
    >>
    >> is_approximately ( double what )
    >> : x ( what )
    >> {}
    >>
    >> bool operator() ( double y ) const {
    >> // return true if y is near x.
    >> }
    >>
    >> };
    >>
    >> Now you can do:
    >>
    >> find( whatever.begin(), whatever.end(), is_approximately( 0.5 ) );


    So that would be:

    find_if( whatever.begin(), whatever.end(), is_approximately( 0.5 ) );


    Thanks

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 16, 2007
    #4
  5. Another option would also be std::find_first_of.

    Regards
    Klaas
    Klaas Vantournhout, Oct 17, 2007
    #5
    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. Sydex
    Replies:
    12
    Views:
    6,485
    Victor Bazarov
    Feb 17, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,293
    Pete Becker
    Mar 30, 2005
  3. Ingo Nolden
    Replies:
    15
    Views:
    1,524
    Jerry Coffin
    Apr 30, 2005
  4. Jason Heyes
    Replies:
    8
    Views:
    721
    Andrew Koenig
    Jan 15, 2006
  5. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
Loading...

Share This Page