[C++] std::transform 一元、二元函数 →→→→→进入此内容的聊天室

来自 , 2020-12-03, 写在 C++, 查看 102 次.
URL http://www.code666.cn/view/69d1fc78
  1. //  std::transform  一元、二元函数
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. #include <deque>
  6. #include <iostream>
  7. #include <functional>
  8.  
  9. int main() {
  10.         using namespace std;
  11.  
  12.         string strSample("THIS is a TEst string!");
  13.         cout << "The sample string is: " << strSample << endl;
  14.  
  15.         string strLowerCaseCopy;
  16.         strLowerCaseCopy.resize(strSample.size());
  17.  
  18.         transform(strSample.begin() // start of source range
  19.                         , strSample.end() // end of source range
  20.                         , strLowerCaseCopy.begin() // start of destination range
  21.                         , tolower); // unary function
  22.  
  23.         cout << "Result of 'transform' on the string with 'tolower':" << endl;
  24.         cout << "\"" << strLowerCaseCopy << "\"" << endl << endl;
  25.  
  26.         // Two sample vectors of integers...
  27.         vector<int> vecIntegers1, vecIntegers2;
  28.         for (int nNum = 0; nNum < 10; ++nNum) {
  29.                 vecIntegers1.push_back(nNum);
  30.                 vecIntegers2.push_back(10 - nNum);
  31.         }
  32.  
  33.         // A destination range for holding the result of addition
  34.         deque<int> dqResultAddition(vecIntegers1.size());
  35.  
  36.         transform(vecIntegers1.begin() // start of source range 1
  37.                         , vecIntegers1.end() // end of source range 1
  38.                         , vecIntegers2.begin() // start of source range 2
  39.                         , dqResultAddition.begin() // start of destination range
  40.                         , plus<int>()); // binary function
  41.  
  42.         cout << "Result of 'transform' using binary function 'plus': " << endl;
  43.         cout << endl << "Index Vector1 + Vector2 = Result (in Deque)" << endl;
  44.         for (size_t nIndex = 0; nIndex < vecIntegers1.size(); ++nIndex) {
  45.                 cout << nIndex << " \t " << vecIntegers1[nIndex] << "\t+ ";
  46.                 cout << vecIntegers2[nIndex] << " \t = ";
  47.  
  48.                 cout << dqResultAddition[nIndex] << endl;
  49.         }
  50.  
  51.         return 0;
  52. }
  53.  

回复 " std::transform 一元、二元函数"

这儿你可以回复上面这条便签

captcha