P

#### Pete

implement library algorithms, trying to minimize the number of iterator

operations. I have two questions so far.

First, it took me a while to figure out how to get accumulate() to compile

correctly:

// begin and begin2 must both be input iterators.

//

template< class In, class Out >

Out accumulate( In begin, const In end, Out )

{

Out ret = 0;

while ( begin != end )

ret += *begin++;

return ret;

}

The problem is that I don't understand the function's signature. When we

define a function, each argument needs a type and an argument name. So this

makes sense to me:

template< class In, class Out >

Out accumulate( In begin, const In end, Out t )

{

...

}

but not this:

template< class In, class Out >

Out accumulate( In begin, const In end, Out )

{

...

}

Of course, the one that makes sense to me doesn't compile. The compiler just

needs the type of the argument; the actual value is inconsequential:

int i = accumulate( v.begin(), v.end(), 0 );

But it still feels wierd to define a function with an argument with a type,

but no name. Is there a way to better understand why the language's syntax

is like this?

Secondly, I'm having trouble with transform().

template< class In >

bool transform( In b, const In e, In dest, In (*f)(In in) );

template< class In >

In mySquare( In arg );

int main( void )

{

vector<int> v1, v2;

for ( vector<int>::size_type i = 1; i <= 10; ++i )

v1.push_back(i);

transform( v1.begin(), v1.end(), back_inserter(v2), mySquare );

return 0;

}

template< class In >

In mySquare( In arg )

{

return arg * arg;

}

// begin and begin2 must both be input iterators.

//

template< class In >

bool transform( In begin, const In end, In dest, In (*f)(In in) )

{

while ( begin != end )

*dest++ = f(*begin++);

return dest;

}

The compiler isn't finding any functions that match my call to transform(),

so I've blown it somewhere. How can this code be fixed?

Thanks,

Peter