<算法>变换修改 [英] <algorithm> transform modification
问题描述
嗨!
我尝试修改变换算法的方式不是
取迭代器,而是对容器的引用class和value,
因为我需要对容器和单个
数进行操作(例如,将向量中的所有值乘以3左右) 。
所以,这是我的意图:
#ifndef ALGORITHM_EXT_HH
#define ALGORITHM_EXT_HH
#include< iterator>
命名空间std
{
模板< typename _Tp,模板< 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,__ value);
返回__cont;
}
} //命名空间std
#endif // ALGORITHM_EXT_HH
我从g ++(GCC)获得的错误消息3.3 20030226(预发布)(SuSE
Linux)是:
linux @ earth:〜/> g ++ mod2pnm.cc -o mod2pnm
mod2pnm.cc:16中包含的文件:
algorithm_ext.hh:函数`_Container< _Tp>&
std :: transform(_Container< _Tp>&,const _Tpval&,_BinaryOperation)'':
algorithm_ext.hh:32:错误:在'=''标记之前解析错误
algorithm_ext.hh:33:错误:在'=''令牌之前解析错误
algorithm_ext.hh:在函数`_Container< _Tp>&
std :: transform(_Container< _Tp>&,const _Tpval&,_BinaryOperation)
[with _Tp
= double,_Container = std :: vector,_Tpval = double,
_BinaryOperation =
std :: minus< double>]'':
mod2pnm.cc:144:从这里实例化
algorithm_ext.hh:34:错误:`__end''未声明(首先使用此
函数)
algorithm_ext.hh:34:错误: (每个未声明的标识符报告
只有一次
对于它出现的每个函数。)
algorithm_ext.hh:34:错误:`__iter''未声明(首先使用这个
函数)
任何想法,什么'错了?我整晚都试过了!
谢谢,Steffen
Steffen Brinkmann写道:#ifndef ALGORITHM_EXT_HH
#define ALGORITHM_EXT_HH
#include< iterator>
命名空间std
{
您不能在std命名空间中放置任何内容!
模板< typename _Tp,模板< 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();
typename _Container< _Tp> :: iterator __iter = __ cont.begin();
_Container< _Tp> :: iterator __end = __ cont.end();
同上
for(; __iter!= __end; ++ __ iter)
* __ iter = __binary_op(* __ iter,__ value);
返回__cont;
}
} //命名空间std
#endif // ALGORITHM_EXT_HH
我从g ++(GCC)获得的错误消息3.3 20030226(预发布) )(SuSE
Linux)是:
[SNIP]
似乎3.3实现了两个阶段名称查找或至少它拒绝
来猜测什么是类型。
-
WW aka Attila
Steffen Brinkmann写道:我尝试修改变换算法的方式是它没有采用迭代器,而是对容器类的引用和值,因为我需要对容器和单个数字进行操作(例如,将向量中的所有值乘以3左右)。
除了WW的好评,我还是我可以补充一下:
您可以在< functional>中试用std :: bind2nd并坚持使用
std :: transform:
std :: transform(v.begin(),v.end(),v.begin( ),
std :: bind2nd(std :: multiplies< int>(),3));
如果你进入更复杂的操作,提升:: bind可能会提供
答案( www.boost.org )。 boost :: bind最终可能是标准化的b $ b。它已被选入第一个图书馆技术报告
,表明该图书馆的官方兴趣。以上
变换转换为bind:
std :: transform(v.begin(),v.end(),v.begin() ,
boost :: bind(std :: multiplies< int>(),_1,3));
-Howard
" WW" < WO *** @ freemail.hu>在消息中写道
news:bk ********** @ phys-news1.kolumbus.fi ...[...]
您不能在std
命名空间中放置任何内容!
[...]
除了std :: less<的特化之外;>,也许还有其他一些
的东西。
Dave
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
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
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
"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
这篇关于<算法>变换修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!