<algorithm> transform modification

S

Steffen Brinkmann

Hi!

I tried to modify the transform algorithm in a way that it doesn't
take iterators, but a reference to a container class and a value,
because Mostly I need to do an operation of a container and a single
number (e.g. multiply all the values in a vector by 3 or so).
So, this is my intent:

#ifndef ALGORITHM_EXT_HH
#define ALGORITHM_EXT_HH

#include<iterator>

namespace std
{
template<typename _Tp, template<typename> class _Container,
typename _Tpval, typename _BinaryOperation>
_Container<_Tp>&
transform(_Container<_Tp>& __cont, const _Tpval& __val,
_BinaryOperation __binary_op)
{
_Container<_Tp>::iterator __iter=__cont.begin();
_Container<_Tp>::iterator __end=__cont.end();
for ( ; __iter != __end; ++__iter)
*__iter = __binary_op(*__iter, __val);
return __cont;
}

} //namespace std

#endif //ALGORITHM_EXT_HH

The error message I get from g++ (GCC) 3.3 20030226 (prerelease) (SuSE
Linux) is:


linux@earth:~/> g++ mod2pnm.cc -o mod2pnm
In file included from mod2pnm.cc:16:
algorithm_ext.hh: In function `_Container<_Tp>&
std::transform(_Container<_Tp>&, const _Tpval&, _BinaryOperation)':
algorithm_ext.hh:32: error: parse error before `=' token
algorithm_ext.hh:33: error: parse error before `=' token
algorithm_ext.hh: In function `_Container<_Tp>&
std::transform(_Container<_Tp>&, const _Tpval&, _BinaryOperation)
[with _Tp
= double, _Container = std::vector, _Tpval = double,
_BinaryOperation =
std::minus<double>]':
mod2pnm.cc:144: instantiated from here
algorithm_ext.hh:34: error: `__end' undeclared (first use this
function)
algorithm_ext.hh:34: error: (Each undeclared identifier is reported
only once
for each function it appears in.)
algorithm_ext.hh:34: error: `__iter' undeclared (first use this
function)


Any ideas, what's wrong? I tried all night long!

Thanks, Steffen
 
W

WW

Steffen said:
#ifndef ALGORITHM_EXT_HH
#define ALGORITHM_EXT_HH

#include<iterator>

namespace std
{

You are not allowed to place anything into the std namespace!
template<typename _Tp, template<typename> class _Container,

Standard container templates take many more arguments.
typename _Tpval, typename _BinaryOperation>
_Container<_Tp>&
transform(_Container<_Tp>& __cont, const _Tpval& __val,
_BinaryOperation __binary_op)
{
_Container<_Tp>::iterator __iter=__cont.begin();
typename _Container said:
_Container<_Tp>::iterator __end=__cont.end(); Ditto

for ( ; __iter != __end; ++__iter)
*__iter = __binary_op(*__iter, __val);
return __cont;
}

} //namespace std

#endif //ALGORITHM_EXT_HH

The error message I get from g++ (GCC) 3.3 20030226 (prerelease) (SuSE
Linux) is:
[SNIP]

Seems that 3.3 has two phase name lookup implemented or at least it refuses
to guess what is a type anymore.
 
H

Howard Hinnant

Steffen said:
I tried to modify the transform algorithm in a way that it doesn't
take iterators, but a reference to a container class and a value,
because Mostly I need to do an operation of a container and a single
number (e.g. multiply all the values in a vector by 3 or so).

In addition to WW's good comments, I thought I might add:

You might try out std::bind2nd in <functional> and stick with the
std::transform:

std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(std::multiplies<int>(), 3));

If you get into more complicated operations, boost::bind might provide
an answer ( www.boost.org ). boost::bind may eventually be
standardized. It has been voted into the first library technical report
which indicates an official interest in this library. The above
transform translates into bind with:

std::transform(v.begin(), v.end(), v.begin(),
boost::bind(std::multiplies<int>(), _1, 3));

-Howard
 
D

David B. Held

WW said:
[...]
You are not allowed to place anything into the std
namespace!
[...]

Except specializations of std::less<>, and perhaps a few other
things.

Dave
 
J

Jerry Coffin

[ ... ]
You are not allowed to place anything into the std namespace!

Not so -- you are specifically allowed to add (partial or complete)
specializations of standard library templates to namespace std. See $
17.4.3.1/1 for the exact requirements.
 
W

WW

David said:
WW said:
[...]
You are not allowed to place anything into the std
namespace!
[...]

Except specializations of std::less<>, and perhaps a few other
things.

Yeah. But they have to be specializations, not new things. And I prefer to
mentioned them for those, who know they can ask this question. :)
 
W

WW

Jerry said:
[ ... ]
You are not allowed to place anything into the std namespace!

Not so -- you are specifically allowed to add (partial or complete)
specializations of standard library templates to namespace std. See $
17.4.3.1/1 for the exact requirements.

You are not placing them into the standard namespace. They are there and
you are allowed to specialize them. I prefer to put it this way because I
have met people (hearing there is exception) kept putting declarations of
new things there.
 
R

Rob Williscroft

WW wrote in
David said:
WW said:
[...]
You are not allowed to place anything into the std
namespace!
[...]

Except specializations of std::less<>, and perhaps a few other
things.

Yeah. But they have to be specializations, not new things. And I
prefer to mentioned them for those, who know they can ask this
question. :)

IIUC you can specialize *any* template in namespace std aslong as
the specialization is dependant an a UDT not defined in namespace std.
I.e. this should be Ok:

class myclass {};
namespace std
{
template < typename Alloc >
class vector< myclass, Alloc >
{
};
}

Rob.
 
W

WW

Rob Williscroft wrote:
[SNIP]
IIUC you can specialize *any* template in namespace std aslong as
the specialization is dependant an a UDT not defined in namespace std.
I.e. this should be Ok:

class myclass {};
namespace std
{
template < typename Alloc >
class vector< myclass, Alloc >
{
};
}

Chapter and verse?
 
R

Rob Williscroft

WW wrote in
Rob Williscroft wrote:
[SNIP]
IIUC you can specialize *any* template in namespace std aslong as
the specialization is dependant an a UDT not defined in namespace std.
I.e. this should be Ok:

class myclass {};
namespace std
{
template < typename Alloc >
class vector< myclass, Alloc >
{
};
}

Chapter and verse?

17.4.3.1/1 - but I missed "... results in undefined ... and unless the
specialization meets the standard library requirements for the original
...." so my example wouldn't be Ok (that "IIUC" wasn't wasted after all).

Rob.
 
W

WW

Rob Williscroft wrote:
[SNIP]
17.4.3.1/1 - but I missed "... results in undefined ... and unless the
specialization meets the standard library requirements for the
original ..." so my example wouldn't be Ok (that "IIUC" wasn't wasted
after all).

That's what I thought. :)
 
S

Steffen Brinkmann

WW said:
Standard container templates take many more arguments.

I copied that method from Bruce Eckel's "Thinking in C++". An dit
works... Do you mean an explicit Reference?
typename _Container<_Tp>::iterator __iter=__cont.begin();

THANKSALOT! That was it! typical case of blindness after hours of
nightly programming!
Seems that 3.3 has two phase name lookup implemented or at least it refuses
to guess what is a type anymore.

Why?

Thanks again, Steffen
 
S

Steffen Brinkmann

Howard Hinnant said:
You might try out std::bind2nd in <functional> and stick with the
std::transform:

std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(std::multiplies<int>(), 3));

That was my first attempt and it worked fine. but I like
short-to-write function calls like:

If you get into more complicated operations, boost::bind might provide
an answer ( www.boost.org ). boost::bind may eventually be
standardized. It has been voted into the first library technical report
which indicates an official interest in this library. The above
transform translates into bind with:

std::transform(v.begin(), v.end(), v.begin(),
boost::bind(std::multiplies<int>(), _1, 3));

Aha, interesting site, thank you!

Steffen
 
F

Fraser Ross

You might try out std::bind2nd in said:
std::transform:

std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(std::multiplies<int>(), 3));

Is there a reason for not using for_each? I have been wondering if for_each
is faster.

Fraser.
 
J

Jerry Coffin

Where do I find that?

That's section 17.4.3.1, paragraph 1, of the C++ standard. If you don't
have a copy, you can get one as a PDF file for $18US from
webstore.ansi.org (this requires a credit or debit card they'll accept).

The DIN only seems to sell the paper version, at a much less attractive
price (242,70 Euros). You can also get it direct from the ISO (in
either PDF or paper format) for 364 CHF, which is roughly typical for a
paper copy, but clearly pretty high for a PDF.

Various other national bodies sell it as well, but I don't know of any
that sell the PDF for less than ANSI does. If you decided to get the
paper version, shipping costs would probably favor buying locally.
 
H

Howard Hinnant

You might try out std::bind2nd in <functional> and stick with the
std::transform:

std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(std::multiplies<int>(), 3));

Is there a reason for not using for_each? I have been wondering if for_each
is faster.[/QUOTE]

I think it would be a matter of convenience rather than performance.
for_each could certainly be used here. But the use of for_each looks
more complicated to me than does transform.

On the inside transform will do something like:

*target = op(*source);

whereas for_each will do something like:

op(*source);

In our example, we're wanting to do:

*target = *target * 3;

or maybe:

*target *= 3;

So to use for_each you have to cook up a functor that takes a single
argument and performs the desired operation (both times and assign, or
the combined *=). There is no std::multiply_assign, but you could roll
your own:

template <class T, class U = T>
struct multiply_assign
: public std::binary_function<T, U, T>
{
T& operator()(T& t, const U& u) const
{return t *= u;}
};

Then you could use this with for_each and bind2nd like:

std::for_each(v.begin(), v.end(),
std::bind2nd(multiply_assign<int>(), 3));

I would expect this example to have the same performance as the
transform version, but I have not tested that expectation.

-Howard
 
F

Fraser Ross

transform might be as fast if return value optimisation is used. I would
prefer another algorithm for when the output iterator is the same as an
input iterator and two ranges are used. That isn't the situation the OP
has. e.g.

template <class ForwardIterator, class InputIterator, class BinaryOperation>
BinaryOperation transform_each (ForwardIterator first1, InputIterator last1,
InputIterator first2, BinaryOperation binary_op) {
while (first1 != last1) {
binary_op(*first1, *first2);
++first1, ++first2;
}
return binary_op;
}


Fraser.
 
S

Steffen Brinkmann

Sorry, pressed the wrong key.
Is there a reason for not using for_each?

As I said, I'd have to write sth like

void f(double val){ val+=2.;}

and then call

for_each(v.begin(),v.end(),f());

What, if it is noct 2. but a value that is calculated at runtime?

Steffen
 
R

red floyd

Steffen said:
Sorry, pressed the wrong key.



As I said, I'd have to write sth like

void f(double val){ val+=2.;}

and then call

for_each(v.begin(),v.end(),f());

What, if it is noct 2. but a value that is calculated at runtime?

Steffen

use a functor... There's probably a standard one that I can't remember at this time, but here's one,
and I realize I may have the syntax/semantics of for_each() slightly messed up. Please have pity, gurus!

struct add_value {
double val;
add_value (val_) : val(val_) { }
double operator()(double& d) { return d += val; }
}

then you can call:

for_each(v.begin(), v.end(), add_value(some_calculated_value_here));
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top