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 

Re: Warum geht for_each nicht?

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





PostPosted: Fri Sep 05, 2003 2:02 pm    Post subject: Re: Warum geht for_each nicht? Reply with quote



Andreas Nicolai wrote:
Quote:
Der folgende Code compiliert zwar anstandslos (sowohl unter BCC5 als auch
unter gcc 3.1) aber er funktioniert nicht, d.h. das Ergebnis ist 1 und
nicht wie erwartet 2.

Hmm, Rundungsfehler?
SCNR ;-)

Quote:
----
vector<double> vec(10);
fill(vec.begin(), vec.end(), 1.0);

Warum nicht einfach:

vector<double> vec(10, 1.0);

Quote:
double factor;

Uninitialisiert? Tztz... :-)

Quote:
for_each(vec.begin(), vec.end(), bind2nd(multiplies<double>(), factor));
cout << vec[0] << endl;

for_each wendet einfach den Funktor auf jedes "Objekt" im
Iteratorrange an. Das Ergebnis wird dabei aber nicht in den
Iterator geschrieben. In diesem Fall wird für jedes Element
factor * Element berechnet und dann weggeworfen.

Quote:
Wenn ich die for_each Anweisung mit folgender ersetze, funktionierts
tadellos...
----
transform(vec.begin(), vec.end(), vec.begin(),
bind2nd(multiplies ----

transform bekommt einen Quellrange und einen Zielrange. In
den Zielrange werden die Rückgabewerte des Funktors geschrieben.

mfg

Christoph

--
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.