C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Utiliser une fonction dans std::find

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Michaël Delva
Guest





PostPosted: Mon Jul 19, 2004 10:01 am    Post subject: Utiliser une fonction dans std::find Reply with quote



Bonjour à tous,

voici mon problème. J'ai les classes suivantes:

class Test { };

class A
{
bool toto(const Test & t);
};

class B
{
std::vector<A> liste_A;
void foo(const Test & t);
}


void B::foo(const Test & t)
{
std::vector<A>::const_iterator ite = std::find(liste_A.begin(),liste_A.end
(),??);
}

En fait je voudrais que find me renvoie l'itérateur de liste_A qui
corresponde à la condition testée par la fonction A::toto(const Test & t);

Il me semble que je dois utiliser boost pour cela, mais je n'y arrive
pas...

Merci d'avance!!
Back to top
Falk Tannhäuser
Guest





PostPosted: Mon Jul 19, 2004 1:15 pm    Post subject: Re: Utiliser une fonction dans std::find Reply with quote



"Michaël Delva" wrote:
Quote:

class Test { };

class A
{
bool toto(const Test & t);
};

class B
{
std::vector<A> liste_A;
void foo(const Test & t);
}

void B::foo(const Test & t)
{
std::vector<A>::const_iterator ite = std::find(liste_A.begin(),liste_A.end
(),??);
}

En fait je voudrais que find me renvoie l'itérateur de liste_A qui
corresponde à la condition testée par la fonction A::toto(const Test & t);

Malheureusement,
std::vector<A>::const_iterator ite = std::find_if(liste_A.begin(),
liste_A.end(), std::bind2nd(std::mem_fun_ref(&A::toto), t));
ne passe pas parce que toto prend le paramètre Test par référence...

La solution la plus simple me paraît de définir
struct call_A_toto
{
Test const& test;
call_A_toto(Test const& t) : test(t) {}
bool operator()(A& a) const { return a.toto(test); }
};

puis appeler
std::vector<A>::const_iterator ite = std::find_if(liste_A.begin(), liste_A.end(), call_A_toto(t));

Si tu veux t'amuser tu peux essayer de "templatiser" call_A_toto afin qu'il
marche pour n'importe quelle classe / fonction membre.

Quote:
Il me semble que je dois utiliser boost pour cela, mais je n'y arrive
pas...

Je pense que Boost offre une solution permettant de construire un functeur
"à la volée" (un peu comme avec std::mem_fun_ref et std::bind2nd) mais
je n'ai pas eu le temps de regarder...

Falk

Back to top
Michaël Delva
Guest





PostPosted: Tue Jul 20, 2004 8:40 am    Post subject: Re: Utiliser une fonction dans std::find Reply with quote



Quote:
Malheureusement,
std::vector<A>::const_iterator ite = std::find_if(liste_A.begin(),
liste_A.end(), std::bind2nd(std::mem_fun_ref(&A::toto), t));
ne passe pas parce que toto prend le paramètre Test par référence...

La solution la plus simple me paraît de définir
struct call_A_toto
{
Test const& test;
call_A_toto(Test const& t) : test(t) {}
bool operator()(A& a) const { return a.toto(test); }
};

puis appeler
std::vector<A>::const_iterator ite = std::find_if(liste_A.begin(),
liste_A.end(), call_A_toto(t));

Si tu veux t'amuser tu peux essayer de "templatiser" call_A_toto afin
qu'il marche pour n'importe quelle classe / fonction membre.

Il me semble que je dois utiliser boost pour cela, mais je n'y arrive
pas...

Je pense que Boost offre une solution permettant de construire un
functeur "à la volée" (un peu comme avec std::mem_fun_ref et
std::bind2nd) mais je n'ai pas eu le temps de regarder...

Falk

Merci de ta réponse, ça fonctionne bien comme ça ;)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.