for_each function and operator()

G

George2

Hello everyone,


I can not find related information in MSDN, so I want to confirm here,
that,

the implementation of for_each is something like,

Code:
template <class A, class B> B for_each (A begin, A end, B Func)
{
    while (begin != end) Func(*begin++);
    return Func;
}

So, operator(*A) of class Func will be invoked? Right?


thanks in advance,
George
 
R

Rolf Magnus

George2 said:
Hello everyone,


I can not find related information in MSDN, so I want to confirm here,
that,

the implementation of for_each is something like,

Code:
template <class A, class B> B for_each (A begin, A end, B Func)
{
while (begin != end) Func(*begin++);
return Func;
}

So, operator(*A) of class Func will be invoked? Right?

Yes.
 
A

Abhishek Padmanabh

Hello everyone,

I can not find related information in MSDN, so I want to confirm here,
that,

the implementation of for_each is something like,

Code:
template <class A, class B> B for_each (A begin, A end, B Func)
{
while (begin != end) Func(*begin++);
return Func;}

So, operator(*A) of class Func will be invoked? Right?

Not exactly, not operator(*A), but Func(*it) will be called as many
times as elements starting from begin to one before end (end is one
past the last element, so it is exclusive of that). You should not
need to delve into the details of the algorithms to see how to use
them. It should be clear from the documentations that for each of the
elements between begin and end (including begin, excluding end), the
call (a pointer to a function or a function object can be passed)
Func() will be executed with each of those elements as an argument.
Here func would be a unary function, that would accept a single
argument. Hope it helps.
 
V

Victor Bazarov

George2 said:
I can not find related information in MSDN, so I want to confirm here,
that,

the implementation of for_each is something like,

Code:
template <class A, class B> B for_each (A begin, A end, B Func)
{
while (begin != end) Func(*begin++);
return Func;
}

So, operator(*A) of class Func will be invoked? Right?

If 'B' is a class, then yes, operator(). If 'B' is a function type,
then the function 'Func' will be called.

V
 
T

terminator

Hello everyone,

I can not find related information in MSDN, so I want to confirm here,
that,

if your tools are not outdated you can just right click on what you
have typed and select 'go to definition'.
the implementation of for_each is something like,

Code:
template <class A, class B> B for_each (A begin, A end, B Func)
{
while (begin != end) Func(*begin++);
return Func;}

MS actualy devices a for loop instead of while and uses preincrement(+
+it) operation which results in more optimized and readable
implementation.


regards,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top