Skillful Use of Lambda Expressions and STL Algorithms

Go directly to the code, and you can draw inferences from one example:

#include <algorithm>
// eg1:
     std::vector<int> vecIn1(5,3);
     std::sort(vecIn1 .begin(), vecIn1 .end(), [](int t1, int t2)
     {
      return (t1 < t2)? true: false;
     });
//eg2:
     std::vector<int> vecIn1(5,3);
     std::vector<int> vecIn2(5,3);
     std::vector<int> vecOut;
     std::transform(vecIn1.begin(), vecIn1.end(), vecIn2.begin(), vecOut.begin(), std::plus<int>());
//eg3:
     int n = 9;
     std::transform(vecIn1.begin(), vecIn1.end(), vecOut.begin(), [&](int t1)
     {
          return (t1+n);
     });
// eg4:
     float fAreaStd = 30.9f;
     float fThresh = 5.0f;
     vector<float>vecfloat;
     auto it  = std::find(vecfloat.begin(), vecfloat.end(), [&](float f1)
     {
          if (fabs(f1 - fAreaStd) < fThresh)
          {
              return true;
          }
          else
          {
              return false;
          }
     });
     float tFind = *it;
 
     std::vector<float> vecFindRlt;
     int mycount = std::count_if(vecfloat.begin(), vecfloat.end(), [&](float f1)->bool
     {
          if (fabs(f1 - fAreaStd) < fThresh)
          {
              vecFindRlt.push_back(f1);
              return true;
          }
          else
          {
              return false;
          }
     });

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781150&siteId=291194637