C++/C++11中std::transform的使用

std::transform函数是将某操作应用于指定范围的每个元素。要使用std::transform函数需要包含<algorithm>头文件。

以下是对std::transform的解释:

[cpp]  view plain  copy
  1. /* 
  2. // reference: http://en.cppreference.com/w/cpp/algorithm/transform 
  3.  
  4. template< class InputIt, class OutputIt, class UnaryOperation > 
  5. OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op ) 
  6. { 
  7.   while (first1 != last1) { 
  8.   *d_first++ = binary_op(*first1++, *first2++); 
  9.   } 
  10.   return d_first; 
  11. } 
  12.  
  13. template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > 
  14. OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op ); 
  15.  
  16. std::transform applies the given function to a range and stores the result in another range, beginning at d_first. 
  17. (1): The unary operation unary_op is applied to the range defined by [first1, last1). 
  18. (2): The binary operation binary_op is applied to pairs of elements from two ranges: 
  19.      one defined by [first1, last1) and the other beginning at first2. 
  20.  
  21. Parameters: 
  22.   first1, last1: the first range of elements to transform 
  23.   first2: the beginning of the second range of elements to transform 
  24.   d_first:the beginning of the destination range, may be equal to first1 or first2 
  25.   unary_op: unary operation function object that will be applied. 
  26.   binary_op: binary operation function object that will be applied. 
  27. Return value: Output iterator to the element past the last element transformed. 
  28.  
  29. std::for_each: ignores the return value of the function and guarantees order of execution. 
  30. std::transform: assigns the return value to the iterator, and does not guarantee the order of execution. 
  31. */  

以下是std::transform用法举例:

[cpp]  view plain  copy
  1. #include "transform.hpp"  
  2. #include <algorithm> // std::transform  
  3. #include <string>  
  4. #include <cctype> // std::toupper  
  5. #include <iostream>  
  6. #include <vector>  
  7. #include <functional> // std::plus c++14  
  8.   
  9. int test_transform1()  
  10. {  
  11.     std::string s("Hello");  
  12.     std::transform(s.begin(), s.end(), s.begin(),  
  13.         [](unsigned char c) { return std::toupper(c); });  
  14.     std::cout << s << std::endl; // HELLO  
  15.   
  16.     std::transform(s.begin(), s.end(), s.begin(), ::tolower);  
  17.     std::cout << s << std::endl; // hello  
  18.   
  19.     ////////////////////////////////  
  20.     std::vector<int> arr{ 1, 3, 5 };  
  21.     std::vector<int> arr2{ 1, 3, 5 };  
  22.     std::vector<int> arr3{ 1, 3, 5 };  
  23.   
  24.     std::transform(arr.begin(), arr.end(), arr.begin(),  
  25.         [](int d) -> int {return d * 5; }); // for_each  
  26.     for (auto value : arr) {  
  27.         std::cout << value << "    "// 5 15 25  
  28.     }  
  29.     std::cout<<std::endl;  
  30.   
  31.     std::for_each(arr2.begin(), arr2.end(), [](int& a) {a *= 5; });  
  32.     for (auto value : arr2) {  
  33.         std::cout << value << "    "// 5 15 25  
  34.     }  
  35.     std::cout << std::endl;  
  36.   
  37.     for (auto& value : arr3) {  
  38.         value *= 5;  
  39.     }  
  40.     for (auto value : arr3) {  
  41.         std::cout << value << "    "// 5 15 25  
  42.     }  
  43.     std::cout << std::endl;  
  44.   
  45.     std::vector<std::string> names = { "hi""test""foo" };  
  46.     std::vector<std::size_t> name_sizes;  
  47.   
  48.     ///////////////////////////  
  49.     std::transform(names.begin(), names.end(), std::back_inserter(name_sizes),  
  50.         [](std::string name) { return name.size(); });  
  51.     for (auto value : name_sizes) {  
  52.         std::cout << value << "    "// 2 4 3  
  53.     }  
  54.     std::cout << std::endl;  
  55.   
  56.     std::for_each(name_sizes.begin(), name_sizes.end(), [](std::size_t name_size) {  
  57.         std::cout << name_size << "    "// 2 4 3  
  58.     });  
  59.     std::cout << std::endl;  
  60.   
  61.     return 0;  
  62. }  
  63.   
  64. /////////////////////////////////////////////////////////  
  65. // reference: http://www.cplusplus.com/reference/algorithm/transform/  
  66. static int op_increase(int i) { return ++i; }  
  67.   
  68. int test_transform2()  
  69. {  
  70.     std::vector<int> foo;  
  71.     std::vector<int> bar;  
  72.   
  73.     // set some values:  
  74.     for (int i = 1; i<6; i++)  
  75.         foo.push_back(i * 10); // foo: 10 20 30 40 50  
  76.   
  77.     bar.resize(foo.size()); // allocate space  
  78.   
  79.     std::transform(foo.begin(), foo.end(), bar.begin(), op_increase);  
  80.     // bar: 11 21 31 41 51  
  81.   
  82.     // std::plus adds together its two arguments:  
  83.     std::transform(foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());  
  84.     // foo: 21 41 61 81 101  
  85.   
  86.     std::cout << "foo contains:";  
  87.     for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)  
  88.         std::cout << ' ' << *it; // 21 41 61 81 101  
  89.     std::cout << '\n';  
  90.   
  91.     return 0;  
  92. }  

猜你喜欢

转载自blog.csdn.net/jisuanji198509/article/details/80723877