巧用Lambda表达式和STL算法

直接上代码,大家可以举一反三:

#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;
          }
     });

 

posted @ 2018-04-24 16:54  Jane_bai  阅读(317)  评论(0编辑  收藏  举报