std::transform
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <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. std::transform applies the given function to a range and stores the result in another range, beginning at d_first. You can help to correct and verify the translation. Click here for instructions.
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.You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 参数
| first1, last1 | - | 第一个范围的元素进行改造
Original: the first range of elements to transform The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| first2 | - | 变换的元素的第二范围的开头
Original: the beginning of the second range of elements to transform The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| d_first | - | 目标范围的开始,可以等于
first1或first2 Original: the beginning of the destination range, may be equal to first1 or first2 The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| unary_op | - | unary operation function object that will be applied. The signature of the function should be equivalent to the following:
The signature does not need to have const &. | |||||||||
| binary_op | - | binary operation function object that will be applied. The signature of the function should be equivalent to the following:
The signature does not need to have const &. | |||||||||
| Type requirements | |||||||||||
-InputIt must meet the requirements of InputIterator.
| |||||||||||
-InputIt1 must meet the requirements of InputIterator.
| |||||||||||
-InputIt2 must meet the requirements of InputIterator.
| |||||||||||
-OutputIt must meet the requirements of OutputIterator.
| |||||||||||
[编辑] 返回值
You can help to correct and verify the translation. Click here for instructions.
[编辑] 复杂性
1)unary_opunary_opYou can help to correct and verify the translation. Click here for instructions.
binary_opbinary_opYou can help to correct and verify the translation. Click here for instructions.
[编辑] 要求
unary_op和binary_op没有任何副作用。 (至 C++11)unary_op and binary_op have no side effects. (至 C++11)You can help to correct and verify the translation. Click here for instructions.
unary_opbinary_op没有任何迭代器失效,其中包括结束迭代器,或修改任何元素所涉及的范围。 (C++11 起)unary_op and binary_op do not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (C++11 起)You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
| First version |
|---|
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; } |
| Second version |
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; } |
[编辑] 为例
You can help to correct and verify the translation. Click here for instructions.
#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; }
Output:
HELLO
[编辑] 另请参阅
| 某一范围的元素应用一个函数 Original: applies a function to a range of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |