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 

for_each(..) auf "fremde" MemberFunction anwenden

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
chris
Guest





PostPosted: Wed Feb 08, 2006 4:01 pm    Post subject: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote



Hallo!

Ich bekomme folgendes Beispiel nicht kompiliert. Habe mal ein wenig in
diesem Forum gegoogled, aber keine wirklichen Hinweise gefunden.
Sieht jemand eine Lösung?

tia
chris


----8<--------------------------8<--------------------------8<--------------------------8<----------------------
#include <vector>
#include <iostream>
#include <functional>
#include <algorithm>

class TestClass
{
public:
TestClass(){}

void Print(long s)
{
std::cout << s << std::endl;
}
};


void Print(const long& l)
{
std::cout << l << std::endl;
}

int main()
{
typedef void (*fpPrint)(const long&);
fpPrint fpprint = &Print;

typedef void (TestClass::*doPrint)(long);
doPrint doprint = &TestClass::Print;

TestClass a;
std::vector <long> vs(10,999);

std::vector <long>::iterator it = vs.begin();
while (it != vs.end()){
(a.*doprint)(*it);
++it;
}

for_each(vs.begin(), vs.end(), Print); //so gehts auch
for_each(vs.begin(), vs.end(), *fpprint); //so gehts auch
for_each(vs.begin(), vs.end(), *(a.*doprint)); //so wuerde ichs
gerne machen, geht aber nicht
}

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Dietmar Kuehl
Guest





PostPosted: Wed Feb 08, 2006 5:02 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote



chris wrote:
Quote:
for_each(vs.begin(), vs.end(), *(a.*doprint)); //so wuerde ichs
gerne machen, geht aber nicht

So geht das nicht. Ohne eine kleine Änderung an 'Print()' habe ich
es mit dem gcc auf die Schnelle mit dem gcc allerdings nicht hin
bekommen:

#include <vector>
#include <iostream>
#include <functional>
#include <algorithm>

class TestClass
{
public:
TestClass(){}

void Print(long s) const // note: added "const"
{
std::cout << s << std::endl;
}
};


int main()
{
std::vector<long> vs(10, 999);
TestClass a;
for_each(vs.begin(), vs.end(),
std::bind1st(std::mem_fun_ref(&TestClass::Print), a));
}
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
chris
Guest





PostPosted: Wed Feb 08, 2006 6:02 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote



Danke fuer die schnelle Antwort.

Wenn ich allerdings for_each nur auf const Memberfunktionen anwenden
kann, dann sehe ich noch keinen großen Nutzen darin.
Habe hier das Beispiel mit einem kleinen Hack auf die Spitze getrieben.
Allerdings sehe ich am Output noch ein anderes Problem:
Offensichtlich wird das Object a auch noch kopiert. In diesem Fall
schreibe dann wohl doch lieber ein paar Zeilen mehr, als unnötige
temporaries zu erzeugen.

Hier das Beispiel:

--8<------8<------8<------8<------8<------8<------8<------8<------8<------8<------8<------8<----

class TestClass
{
size_t _nCalls;
public:
TestClass():_nCalls(0)
{
std::cout << this << ": ctor" << std::endl;
}
~TestClass(){ std::cout << this << ": called " << _nCalls << "
times" << std::endl; }

void Print(long s) const
{
++const_cast<size_t&>(_nCalls); //HACK
std::cout << s << std::endl;
}
};

int main()
{
std::vector<long> vs(10, 999);
TestClass a;
for_each(vs.begin(), vs.end(),
std::bind1st(std::mem_fun_ref(&TestClass::Print), a));
}

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Gerald Knizia
Guest





PostPosted: Wed Feb 08, 2006 7:10 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

chris schrieb:
Quote:
Wenn ich allerdings for_each nur auf const Memberfunktionen anwenden
kann, dann sehe ich noch keinen großen Nutzen darin.
Habe hier das Beispiel mit einem kleinen Hack auf die Spitze
getrieben. Allerdings sehe ich am Output noch ein anderes Problem:
Offensichtlich wird das Object a auch noch kopiert. In diesem Fall
schreibe dann wohl doch lieber ein paar Zeilen mehr, als unnötige
temporaries zu erzeugen.

Dafür werde ich zwar von den Puristen gesteinigt, aber etwas im Sinne von
#define _for_each( It, Cont ) \
for( (It) = (Cont).begin(); (It) != (Cont).end(); ++(It) )
funktioniert in der Praxis ganz gut. Wenn dann auch endlich mal __typeof()
Operatoren sich in Compilern größerer Verbreitung erfreuen würden, könnte
man sich sogar das vordeklarieren von Iteratoren sparen, wenn man sowas
macht:
#define _for_each( It, Cont ) \
for( __typeof((Cont).begin()) It = (Cont).begin(); It !=
(Cont).end(); ++It ).

In jedem Falle ist gegenwärtig mit der Nichtverwendung von std::for_each und
ähnlich albernen STL Algorithmen im Regelfall sowohl der Lesbarkeit,
Schreibbarkeit als auch der Ausführungsgeschwindigkeit gedient.
--
- C. Gerald Knizia/cgk | #28673212 | this post was made with intention

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Thomas Maeder
Guest





PostPosted: Wed Feb 08, 2006 8:10 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

"chris" <cb@delta-h.de> writes:

Quote:
Offensichtlich wird das Object a auch noch kopiert. In diesem Fall
schreibe dann wohl doch lieber ein paar Zeilen mehr, als unnötige
temporaries zu erzeugen.

Warum denn?

std::for_each(vs.begin(), vs.end(),
std::bind1st(std::mem_fun(&TestClass::Print), &a));

reicht. Das sind sogar 3 Zeichen weniger!

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
chris
Guest





PostPosted: Wed Feb 08, 2006 10:10 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

Quote:
reicht. Das sind sogar 3 Zeichen weniger!

Und das const Problem ist auch gelöst!!! Also spare ich noch einige
Zeichen in der TestClass::Print() Methode Wink
Astrein!

Vielen Dank an alle.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Olaf Krzikalla
Guest





PostPosted: Thu Feb 09, 2006 11:06 am    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

Hi,

Gerald Knizia wrote:
Quote:
Dafür werde ich zwar von den Puristen gesteinigt,
Naja, seit BOOST_FOREACH sollten diese Zeiten vorbei sein.


MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Jan Boehme
Guest





PostPosted: Fri Feb 10, 2006 2:06 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

Dietmar Kuehl wrote:
Quote:
chris wrote:
for_each(vs.begin(), vs.end(), *(a.*doprint)); //so wuerde ichs
gerne machen, geht aber nicht

So geht das nicht. Ohne eine kleine Änderung an 'Print()' habe ich
es mit dem gcc auf die Schnelle mit dem gcc allerdings nicht hin
bekommen:

#include <vector
#include <iostream
#include <functional
#include <algorithm

class TestClass
{
public:
TestClass(){}

void Print(long s) const // note: added "const"
{
std::cout << s << std::endl;
}
};


int main()
{
std::vector<long> vs(10, 999);
TestClass a;
for_each(vs.begin(), vs.end(),
std::bind1st(std::mem_fun_ref(&TestClass::Print), a));
}

Hallo!
Ich hätte da gleich noch eine Anschlussfrage:
Wie sieht es aus, wenn ich einen Vector von TestClass-Objeckten habe und
deren Print-Methoden sequentiell mit einem gleich bleibenden Wert
aufrufen möchte (ohne Umweg über ein Funktion-Objekt).
Ist sowas auch per for_each möglich?
Danke für 'ne Antwort, Jan.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Thomas Maeder
Guest





PostPosted: Fri Feb 10, 2006 6:06 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

Jan Boehme <aiscape (AT) hotmail (DOT) com> writes:

Quote:
Dietmar Kuehl wrote:
chris wrote:
for_each(vs.begin(), vs.end(), *(a.*doprint)); //so wuerde ichs
gerne machen, geht aber nicht
So geht das nicht. Ohne eine kleine Änderung an 'Print()' habe ich
es mit dem gcc auf die Schnelle mit dem gcc allerdings nicht hin
bekommen:
#include <vector
#include <iostream
#include <functional
#include <algorithm
class TestClass
{
public:
TestClass(){}
void Print(long s) const // note: added "const"
{
std::cout << s << std::endl;
}
};
int main()
{
std::vector<long> vs(10, 999);
TestClass a;
for_each(vs.begin(), vs.end(),
std::bind1st(std::mem_fun_ref(&TestClass::Print), a));
}

Hallo!
Ich hätte da gleich noch eine Anschlussfrage:
Wie sieht es aus, wenn ich einen Vector von TestClass-Objeckten habe
und deren Print-Methoden sequentiell mit einem gleich bleibenden Wert
aufrufen möchte (ohne Umweg über ein Funktion-Objekt).
Ist sowas auch per for_each möglich?

Ja.

std::vector<TestClass> vs(10);
for_each(vs.begin(), vs.end(),
std::bind2nd(std::mem_fun_ref(&TestClass::Print), 3));

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
chris
Guest





PostPosted: Fri Feb 10, 2006 7:06 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

Das kannst Du allerdings vergessen, wenn Du Referenzen in der der
Parameterliste benutzt:

void TestClass::Print(const long& s){}

funktioniert nicht.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Thomas Maeder
Guest





PostPosted: Fri Feb 10, 2006 8:06 pm    Post subject: Re: for_each(..) auf "fremde" MemberFunction anwenden Reply with quote

"chris" <cb@delta-h.de> writes:

Quote:
Das kannst Du allerdings vergessen, wenn Du Referenzen in der der
Parameterliste benutzt:

void TestClass::Print(const long& s){}

funktioniert nicht.

Stimmt. Dann muss man auf die Boost Library (http://www.boost.org/)
ausweichen.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) 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.