zoukankan      html  css  js  c++  java
  • 25.不改变原生数据的STL algorithm

    • 通过仿函数for_each操作
      1 vector<int> myv{ 1,2,3,4,5 };
      2 list<double> db{ 1.1,2.2,3.3,4.4,5.5 };
      3 
      4 //循环算法,算法的泛型
      5 print p = for_each(db.begin(), db.end(), print());
      6 cout << p.count << endl;
    • find_if查找算法
      1 //查找算法
      2     auto it = find(myv.begin(), myv.end(), 3);
      3     cout << *it << endl;
    • find_if与lambda结合
       1 //寻找第一个偶数的位置,返回0就是找到的
       2     auto it = find_if(myv.begin(), myv.end(), [](int x)->bool 
       3     {
       4         if (x % 2 == 0)
       5         {
       6             return 0;
       7         }
       8         else
       9         {
      10             return 1;
      11         }
      12     });
      13 
      14     if (it != myv.end())
      15     {
      16         cout << *it << "  pos=" << it - myv.begin() << endl;
      17     }
    • adjacent_find处理相邻的两个数据
      1 list <int> mylist{ 3,6,9,11,11,28,20,29 };
      2     //查找相邻的两个元素相等的位置(adjacent_find处理相邻的两个数据)
      3     auto it = adjacent_find(mylist.begin(), mylist.end());
      4     if (it != mylist.end())
      5     {
      6         cout << *it << endl;
      7         it++;
      8         cout << *it << endl;
      9     }
    • adjacent_find与lambda结合
       1 //寻找第一个奇偶性不同的
       2         auto it = adjacent_find(mylist.begin(), mylist.end(), [](int x,int y)->bool
       3         {
       4             if ((x - y) % 2 == 0)
       5             {
       6                 return 0;
       7             }
       8             else
       9             {
      10                 return 1;
      11             }
      12         });
      13         if (it != mylist.end())
      14         {
      15             cout << *it << endl;
      16             it++;
      17             cout << *it << endl;
      18         }
    • find_first_of寻找第一个集合在第二个集合中出现的第一个数据
       1 char *str1 = "1234567890";
       2     char *str2 = "abcdefg123";
       3     //寻找在string1中第一个出现在string2的字符
       4     char *p = find_first_of(str1, str1 + strlen(str1), str2, str2 + strlen(str2));
       5     cout << *p << endl;
       6 
       7     vector<int> myint1{ 1,2,3,4,5 };
       8     vector<int> myint2{ 7,8,9,10,1 };
       9     auto it = find_first_of(myint1.begin(), myint1.end(), myint2.begin(), myint2.end());
      10     cout << *it << endl;
    • count与count_if查询数据个数
       1 vector<int> myint{ 1,2,3,4,5,6,1,2,3,4 };
       2     int count1 = count(myint.begin(), myint.end(), 3);
       3     cout << count1 << endl;
       4 
       5     //查询所有小于4的元素的个数
       6     int count2 = count_if(myint.begin(), myint.end(), [](int x)
       7     {
       8         if (x < 4)
       9         {
      10             return 1;
      11         }
      12         else
      13         {
      14             return 0;
      15         }
      16     });
      17     cout << count2 << endl;
    • mismatch判断两个集合是否相等
       1 vector<int> myint1{ 1,2,3,4,5,6,1,2,3,4 };
       2     vector<int> myint2{ 1,2,3,4,5,6,1,2,3,4 };
       3 
       4     auto it = mismatch(myint1.begin(), myint1.end(), myint2.begin());
       5     if (it.first == myint1.end() && it.second == myint2.end())
       6     {
       7         cout << "相等" << endl;
       8     }
       9     else
      10     {
      11         //first是第一个容器不匹配的位置(可能为空),second是第二个容器不匹配的位置(可能为空)
      12         cout << "不相等" << endl;
      13         cout << *(it.first) <<"  " << *(it.second) << endl;
      14     }
       1 char *s1[] = { "abc","acv","adf","oop" };
       2     char *s2[] = { "abc","acv","adf","oop","134" };
       3     auto it = mismatch(s1, s1 + 4, s2, [](const char* str1,const char* str2)
       4     {
       5         if (strcmp(str1, str2) == 0)
       6         {
       7             return 1;
       8         }
       9         else
      10         {
      11             return 0;
      12         }
      13     });
      14 
      15     if (it.first == s1 + sizeof(s1)/sizeof(s1[0]) && it.second == s2 + sizeof(s2) / sizeof(s2[0]))
      16     {
      17         cout << "相等" << endl;
      18     }
      19     else
      20     {
      21         //first是第一个容器不匹配的位置(可能为空),second是第二个容器不匹配的位置(可能为空)
      22         cout << "不相等" << endl;
      23         //cout << *(it.first) << "  " << *(it.second) << endl;
      24     } 
    • equal判断是否是一样的集合
       1     
       2 
       3     vector<int> myv1{ 1,2,3,4,5 };
       4     vector<int> myv2{ -1,2,-3,4,-5 };
       5 
       6     //判断绝对值知否相等
       7     if (equal(myv1.begin(), myv1.end(), myv2.begin(), [](int a, int b) ->bool
       8     {
       9         if (a == abs(b) || b == abs(a))
      10         {
      11             return 1;
      12         }
      13         else
      14         {
      15             return 0;
      16         }
      17     }))
      18     {
      19         cout << "相等" << endl;
      20     }
      21     else
      22     {
      23         cout << "不相等" << endl;
      24     }
    • search判断有没有连续一样的部分
       1 vector<int> myv1{ 1,2 };
       2     vector<int > myv2{ 1,2,3,4,5 };
       3     
       4     //判断有没有连续相等的子集
       5     auto it = search(myv1.begin(), myv1.end(), myv2.begin(), myv2.end());
       6 
       7     if (it == myv1.end())
       8     {
       9         cout << "是子集" << endl;
      10     }
      11     else
      12     {
      13         cout << "不是子集" << endl;
      14     }
    • search_n判断有没有连续一样的数据
       1 vector<int> myv1{ 1,2,2,2,3,3,4 };
       2     
       3     //判断有没有连续相等的数据
       4     auto it = search_n(myv1.begin(), myv1.end(),3, 2);
       5     
       6     if (it == myv1.end())
       7     {
       8         cout << "" << endl;
       9     }
      10     else
      11     {
      12         cout << "没有" << endl;
      13     }
    • 从反向寻找一个集合在另一个集合中出现的位置
      1 vector<int> myv2{ 1,2,2,2,3,3,4 };
      2     vector<int> myv3{ 3,4 };
      3     //在myv2中从后往前找myv3所在的位置
      4     auto it = find_end(myv2.begin(), myv2.end(), myv3.begin(), myv3.end());
      5 
      6     if(it != myv2.end())
      7     {
      8         cout << *it << "   " << it - myv2.begin() << endl;
      9     }
  • 相关阅读:
    Action<T>和Func<T>委托
    异步委托学习笔记
    .Net身份验证里边的几个基本概念(转)
    线程和进程
    WebClient类的使用
    关于NHibernate的更新和读取操作,及碰见的一点问题
    ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)
    LINQ标准查询操作符学习笔记
    C#3.0中的扩展方法
    NHibernate中的一对多映射
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8642286.html
Copyright © 2011-2022 走看看