std::transform
| 
   | 
  该页由英文版wiki使用Google Translate机器翻译而来。 
 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里.  | 
|   定义于头文件  <algorithm>
  | 
||
|   template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,  | 
(1) | |
|   template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,   | 
(2) | |
std::transform施加给定的功能的范围内,并存储在另一个范围内的结果,开始在d_first. unary_op被施加到限定的范围内由[first1, last1)。在第二个版本的二进制运算binary_op元素对被施加到从两个范围:1定义的和其他的开始[first1, last1)first2.unary_op is applied to the range defined by [first1, last1). In the second version the binary operation binary_op is applied to pairs of elements from two ranges: one defined by [first1, last1) and the other beginning at first2.目录 | 
[编辑] 参数
| first1, last1 | - | |
| first2 | - | |
| d_first | - | |
| unary_op | - |   unary operation function object that will be applied.  The signature of the function should be equivalent to the following: Ret fun(const Type &a); The signature does not need to have const &.   | 
| binary_op | - |   被使用的二元函数对象。 该函数的签名应当等价于: Ret fun(const Type1 &a, const Type2 &b); 签名中并不需要有 const &。  | 
| 类型要求 | ||
 -InputIt 必须满足  InputIterator 的要求。
 | ||
 -InputIt1 必须满足  InputIterator 的要求。
 | ||
 -InputIt2 必须满足  InputIterator 的要求。
 | ||
 -OutputIt 必须满足  OutputIterator 的要求。
 | ||
[编辑] 返回值
[编辑] 复杂度
1) 2)binary_opbinary_op[编辑] 要求
unary_op和binary_op没有任何副作用。 (C++11 前)unary_opbinary_op没有任何迭代器失效,其中包括结束迭代器,或修改任何元素所涉及的范围。 (C++11 起)[编辑] 可能的实现
| 版本一 | 
|---|
template<class InputIt, class OutputIt, class UnaryOperation> OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op) { while (first1 != last1) { *d_first++ = unary_op(*first1++); } return d_first; }  | 
| 版本二 | 
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation> OutputIt transform(InputIt first1, InputIt last1, InputIt first2, OutputIt d_first, BinaryOperation binary_op) { while (first1 != last1) { *d_first++ = binary_op(*first1++, *first2++); } return d_first; }  | 
[编辑] 示例
#include <string> #include <cctype> #include <algorithm> #include <iostream> int main() { std::string s("hello"); std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper); std::cout << s; }
输出:
HELLO
[编辑] 另请参阅
|    将一个函数应用于某一范围的元素  (函数模板)  |