std::transform在指定的范圍內應用於給定的操作,並將結果存儲在指定的另一個范圍內。要使用std::transform函數需要包含<algorithm>頭文件。
以下是std::transform的兩個聲明,一個是對應於一元操作,一個是對應於二元操作:
template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);
對於一元操作,將op應用於[first1, last1)范圍內的每個元素,並將每個操作返回的值存儲在以result開頭的范圍內。給定的op將被連續調用last1-first1次。op可以是函數指針或函數對象或lambda表達式。
如op的一個實現 即將[first1, last1)范圍內的每個元素加5,然后依次存儲到result中。
int op_increase(int i) {return (i + 5)};
調用std::transform的方式如下:
std::transform(first1, last1, result, op_increase);
對於二元操作,使用[first1, last1)范圍內的每個元素作為第一個參數調用binary_op,並以first2開頭的范圍內的每個元素作為第二個參數調用binary_op,每次調用返回的值都存儲在以result開頭的范圍內。給定的binary_op將被連續調用last1-first1次。binary_op可以是函數指針或函數對象或lambda表達式。
如binary_op的一個實現即將first1和first2開頭的范圍內的每個元素相加,然后依次存儲到result中。
int op_add(int, a, int b) {return (a + b)};
std::transform支持in place,即result和first1指向的位置可以是相同的。std::transform的主要作用應該就是省去了我們自己寫for循環實現。
以下是摘自對std::transform的英文解釋:
/* // reference: http://en.cppreference.com/w/cpp/algorithm/transform template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op ) { while (first1 != last1) { *d_first++ = binary_op(*first1++, *first2++); } return d_first; } template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op ); std::transform applies the given function to a range and stores the result in another range, beginning at d_first. (1): The unary operation unary_op is applied to the range defined by [first1, last1). (2): 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. Parameters: first1, last1: the first range of elements to transform first2: the beginning of the second range of elements to transform d_first:the beginning of the destination range, may be equal to first1 or first2 unary_op: unary operation function object that will be applied. binary_op: binary operation function object that will be applied. Return value: Output iterator to the element past the last element transformed. std::for_each: ignores the return value of the function and guarantees order of execution. std::transform: assigns the return value to the iterator, and does not guarantee the order of execution. */
以下是std::transform用法舉例:
#include "transform.hpp" #include <algorithm> // std::transform
#include <string> #include <cctype> // std::toupper
#include <iostream> #include <vector> #include <functional> // std::plus c++14
int test_transform1() { std::string s("Hello"); std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); }); std::cout << s << std::endl; // HELLO
std::transform(s.begin(), s.end(), s.begin(), ::tolower); std::cout << s << std::endl; // hello
////////////////////////////////
std::vector<int> arr{ 1, 3, 5 }; std::vector<int> arr2{ 1, 3, 5 }; std::vector<int> arr3{ 1, 3, 5 }; std::transform(arr.begin(), arr.end(), arr.begin(), [](int d) -> int {return d * 5; }); // for_each
for (auto value : arr) { std::cout << value << " "; // 5 15 25
} std::cout<<std::endl; std::for_each(arr2.begin(), arr2.end(), [](int& a) {a *= 5; }); for (auto value : arr2) { std::cout << value << " "; // 5 15 25
} std::cout << std::endl; for (auto& value : arr3) { value *= 5; } for (auto value : arr3) { std::cout << value << " "; // 5 15 25
} std::cout << std::endl; std::vector<std::string> names = { "hi", "test", "foo" }; std::vector<std::size_t> name_sizes; /////////////////////////// std::transform(names.begin(), names.end(), std::back_inserter(name_sizes), [](std::string name) { return name.size(); }); for (auto value : name_sizes) { std::cout << value << " "; // 2 4 3
} std::cout << std::endl; std::for_each(name_sizes.begin(), name_sizes.end(), [](std::size_t name_size) { std::cout << name_size << " "; // 2 4 3
}); std::cout << std::endl; return 0; } ///////////////////////////////////////////////////////// // reference: http://www.cplusplus.com/reference/algorithm/transform/
static int op_increase(int i) { return ++i; } int test_transform2() { std::vector<int> foo; std::vector<int> bar; // set some values:
for (int i = 1; i<6; i++) foo.push_back(i * 10); // foo: 10 20 30 40 50
bar.resize(foo.size()); // allocate space
std::transform(foo.begin(), foo.end(), bar.begin(), op_increase); // bar: 11 21 31 41 51 // std::plus adds together its two arguments:
std::transform(foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>()); // foo: 21 41 61 81 101
std::cout << "foo contains:"; for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it) std::cout << ' ' << *it; // 21 41 61 81 101
std::cout << '\n'; return 0; }