B
baibaichen
hi, i need to implement several functions which are similar with
transform, but just oprerate on the elements that match some condition,
i have wirtten one by myself:
template<class In,class Out,class Pred class Op>
Out transform_if(In first,In last,Out res,Pred p,Op op)
{
while(first!=last){
if (p(*first))
*res = op(*first);
++first;++res;
}
return res;
}
but it is something subtle:
1). if the prediction return false? does output iterator increase?
2). how about exception safe?
3). efficiency problem, if we can predict the input will match the
codition, then we have gotten the information that functor will do.
my question is there any better way?
transform, but just oprerate on the elements that match some condition,
i have wirtten one by myself:
template<class In,class Out,class Pred class Op>
Out transform_if(In first,In last,Out res,Pred p,Op op)
{
while(first!=last){
if (p(*first))
*res = op(*first);
++first;++res;
}
return res;
}
but it is something subtle:
1). if the prediction return false? does output iterator increase?
2). how about exception safe?
3). efficiency problem, if we can predict the input will match the
codition, then we have gotten the information that functor will do.
my question is there any better way?