Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > <algorithm> transform modification

Reply
Thread Tools

<algorithm> transform modification

 
 
Steffen Brinkmann
Guest
Posts: n/a
 
      09-20-2003
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
 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      09-20-2003
Steffen Brinkmann wrote:
> #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<_Tp>::iterator __iter=__cont.begin();

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

--
WW aka Attila


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      09-21-2003
Steffen Brinkmann wrote:
> 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
 
Reply With Quote
 
David B. Held
Guest
Posts: n/a
 
      09-21-2003
"WW" <(E-Mail Removed)> wrote in message
news:bkipak$5q2$(E-Mail Removed)...
> [...]
> You are not allowed to place anything into the std
> namespace!
> [...]


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

Dave


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-21-2003
In article <bkipak$5q2$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...

[ ... ]

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

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      09-21-2003
David B. Held wrote:
> "WW" <(E-Mail Removed)> wrote in message
> news:bkipak$5q2$(E-Mail Removed)...
>> [...]
>> 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.

--
WW aka Attila


 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      09-21-2003
Jerry Coffin wrote:
> In article <bkipak$5q2$(E-Mail Removed)>, (E-Mail Removed)
> says...
>
> [ ... ]
>
>> 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.

--
WW aka Attila


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      09-21-2003
WW wrote in news:bkjlf4$a3g$(E-Mail Removed):

> David B. Held wrote:
>> "WW" <(E-Mail Removed)> wrote in message
>> news:bkipak$5q2$(E-Mail Removed)...
>>> [...]
>>> 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.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      09-21-2003
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?

--
WW aka Attila


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      09-21-2003
WW wrote in news:bkk5ha$5ne$(E-Mail Removed):

> 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.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
DNS Reply Modification (doctoring) intermittently failing Dav0 Cisco 1 01-28-2005 07:00 PM
Modification of Duty Cycle - Possible? Drew VHDL 2 08-02-2004 06:54 PM
Schema Modification Error Calum Michie MCSE 1 12-12-2003 03:27 PM
modification : where can i find MOC ? bruno MCSE 3 09-19-2003 11:27 AM



Advertisments