Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using std::find on a std::vector<double>

Reply
Thread Tools

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

 
 
Anonymous
Guest
Posts: n/a
 
      10-16-2007
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?)
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-16-2007
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      10-16-2007
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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-16-2007
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
 
Reply With Quote
 
Klaas Vantournhout
Guest
Posts: n/a
 
      10-17-2007
Another option would also be std::find_first_of.

Regards
Klaas
 
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
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments