473,545 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<algorithm> transform modification

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_H H
#define ALGORITHM_EXT_H H

#include<iterat or>

namespace std
{
template<typena me _Tp, template<typena me> class _Container,
typename _Tpval, typename _BinaryOperatio n>
_Container<_Tp> &
transform(_Cont ainer<_Tp>& __cont, const _Tpval& __val,
_BinaryOperatio n __binary_op)
{
_Container<_Tp> ::iterator __iter=__cont.b egin();
_Container<_Tp> ::iterator __end=__cont.en d();
for ( ; __iter != __end; ++__iter)
*__iter = __binary_op(*__ iter, __val);
return __cont;
}

} //namespace std

#endif //ALGORITHM_EXT_H H

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.h h: In function `_Container<_Tp >&
std::transform( _Container<_Tp> &, const _Tpval&, _BinaryOperatio n)':
algorithm_ext.h h:32: error: parse error before `=' token
algorithm_ext.h h:33: error: parse error before `=' token
algorithm_ext.h h: In function `_Container<_Tp >&
std::transform( _Container<_Tp> &, const _Tpval&, _BinaryOperatio n)
[with _Tp
= double, _Container = std::vector, _Tpval = double,
_BinaryOperatio n =
std::minus<doub le>]':
mod2pnm.cc:144: instantiated from here
algorithm_ext.h h:34: error: `__end' undeclared (first use this
function)
algorithm_ext.h h:34: error: (Each undeclared identifier is reported
only once
for each function it appears in.)
algorithm_ext.h h:34: error: `__iter' undeclared (first use this
function)
Any ideas, what's wrong? I tried all night long!

Thanks, Steffen
Jul 19 '05 #1
20 3814
WW
Steffen Brinkmann wrote:
#ifndef ALGORITHM_EXT_H H
#define ALGORITHM_EXT_H H

#include<iterat or>

namespace std
{
You are not allowed to place anything into the std namespace!
template<typena me _Tp, template<typena me> class _Container,
Standard container templates take many more arguments.
typename _Tpval, typename _BinaryOperatio n>
_Container<_Tp> &
transform(_Cont ainer<_Tp>& __cont, const _Tpval& __val,
_BinaryOperatio n __binary_op)
{
_Container<_Tp> ::iterator __iter=__cont.b egin(); typename _Container<_Tp> ::iterator __iter=__cont.b egin();
_Container<_Tp> ::iterator __end=__cont.en d(); Ditto
for ( ; __iter != __end; ++__iter)
*__iter = __binary_op(*__ iter, __val);
return __cont;
}

} //namespace std

#endif //ALGORITHM_EXT_H H

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
Jul 19 '05 #2
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(st d::multiplies<i nt>(), 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<in t>(), _1, 3));

-Howard
Jul 19 '05 #3
"WW" <wo***@freemail .hu> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
[...]
You are not allowed to place anything into the std
namespace!
[...]


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

Dave
Jul 19 '05 #4
In article <bk**********@p hys-news1.kolumbus. fi>, wo***@freemail. hu
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.
Jul 19 '05 #5
WW
David B. Held wrote:
"WW" <wo***@freemail .hu> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
[...]
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
Jul 19 '05 #6
WW
Jerry Coffin wrote:
In article <bk**********@p hys-news1.kolumbus. fi>, wo***@freemail. hu
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
Jul 19 '05 #7
WW wrote in news:bk******** **@phys-news1.kolumbus. fi:
David B. Held wrote:
"WW" <wo***@freemail .hu> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
[...]
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/
Jul 19 '05 #8
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?

--
WW aka Attila
Jul 19 '05 #9
WW wrote in news:bk******** **@phys-news1.kolumbus. fi:
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/
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4309
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a vector of boost::shared_ptr's, some of which may be NULL. I'd like to find out if there are any non-NULL elements in the vector. Here's what I have at...
7
2596
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
10
6051
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
11
2959
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string> 2 #include <iostream> 3 #include <algorithm> 4 #include <cctype> 5 using namespace std;
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.