By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?)
Oct 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Oct 16 '07 #2

P: n/a
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
Oct 16 '07 #3

P: n/a
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
Oct 16 '07 #4

P: n/a
Another option would also be std::find_first_of.

Regards
Klaas
Oct 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.