最佳STL变换 - 像模板函数的三元运营商 [英] Best STL transform - like template function for ternary operators
问题描述
STL定义变换功能
首先是一元运算符:
模板<类InputIterator的,一流的输出迭代器,类UnaryOperation>
输出迭代器变换(InputIterator的first1,InputIterator的last1,
输出迭代器的结果,UnaryOperation OP);
和第二个是二元运算符:
模板<类InputIterator1,类InputIterator2,
一流的输出迭代器,类BinaryOperation>
输出迭代器变换(InputIterator1 first1,InputIterator1 last1,
InputIterator2 first2,输出迭代器的结果,
BinaryOperation binary_op);
什么是三元运营商最有效的执行一个类似的功能?
修改: 下面是简单的实现我想到了,但是是不是有一个更精简,更优雅的解决方案?
模板<类InputIterator1,类InputIterator2,类InputIterator3,
一流的输出迭代器,类TrenaryOperation>
输出迭代器transform3(InputIterator1 first1,InputIterator1 last1,
InputIterator2 first2,InputIterator3 first3,输出迭代器的结果,
TrenaryOperation trenary_op)
{
而(first1!= last1){
*结果= trenary_op(* first1,* first2,* first3);
++结果; ++ first1; ++ first2; ++ first3;
}
返回结果;
}
这方面的一个简单的版本就可以实现创建n元变换是这样的:
模板<类仿函数,类输出迭代器,
类输入1,类...输入>
输出迭代器变换(函子楼输出迭代器了,
输入1 first1,输入1 last1,
输入...输入)
{
而(first1!= last1)
*出++ = F(* first1 ++,*输入+ ...);
返回了;
}
该版本的尝试逗留接近现有的转换
越好,以一个第一
/ 最后
对迭代器,其余都只是第一
秒。这使得它给用户,以确保所有的范围是有效的,就像用二进制变换
至于性能,我同意ShighShagh的评论对性能不太可能成为一个问题在这里。编译器将是一个更好的地方比你来决定采取什么样的优化,因为每个实例化可能导致不同的情况下,程序员不可能知道在写这个功能。
STL defines two flavors of the transform function
The first is For unary operators:
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
And the second is for binary operators:
template <class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperation>
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);
What is the most efficient implementation of a similiar function for a ternary operator?
EDIT: Here is the trivial implementation I came up with, but isn't there a leaner and more elegant solution?
template <class InputIterator1, class InputIterator2, class InputIterator3,
class OutputIterator, class TrenaryOperation>
OutputIterator transform3(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator3 first3, OutputIterator result,
TrenaryOperation trenary_op)
{
while (first1 != last1) {
*result = trenary_op(*first1, *first2, *first3);
++result; ++first1; ++first2; ++first3;
}
return result;
}
A simple version of this can be achieved to create an n-ary transform like this:
template <class Functor, class OutputIterator,
class Input1, class ... Inputs>
OutputIterator transform(Functor f, OutputIterator out,
Input1 first1, Input1 last1,
Inputs ... inputs)
{
while(first1 != last1)
*out++ = f(*first1++, *inputs++...);
return out;
}
This version tries to stay as close to the existing transform
as possible, taking one first
/last
pair of iterators and the rest are just first
s. This leaves it up to the user to make sure all the ranges are valid, just as with the binary-transform.
As for performance, I agree with ShighShagh's comment about performance not likely being an issue here. The compiler will be in a better place than you to determine what optimizations to take because each instantiation could lead to different situations that the programmer couldn't possibly know while writing this function.
这篇关于最佳STL变换 - 像模板函数的三元运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!