PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > using std::find on a std::vector<double>
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
using std::find on a std::vector<double>

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 04h03   #1
Anonymous
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using std::find on a std::vector<double>

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?)
  Réponse avec citation
Vieux 16/10/2007, 05h17   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using std::find on a std::vector<double>

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
  Réponse avec citation
Vieux 16/10/2007, 05h40   #3
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using std::find on a std::vector<double>

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

  Réponse avec citation
Vieux 16/10/2007, 05h57   #4
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using std::find on a std::vector<double>

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
  Réponse avec citation
Vieux 17/10/2007, 08h27   #5
Klaas Vantournhout
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using std::find on a std::vector<double>

Another option would also be std::find_first_of.

Regards
Klaas
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09735 seconds with 13 queries