zoukankan      html  css  js  c++  java
  • Algorithm常用函数(1)

    1.求和——accumulate

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 
     5 #include<numeric>//accumulate
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     int all[100] = {2,2,3,4,5,6,7,8,9,10};
    11     int sum = accumulate(all, all + 3, 1);//all的前三个数相加,和的初始值是1
    12     printf("%d", sum);
    13 
    14     return 0;
    15 }

    2.二分查找—— binary_search

     1 #include<algorithm>
     2 #include<numeric>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int all[100] = {0, 1, 2, 3, 4, 5, 89, 104, 300, 600, 9000};
     9     int a;
    10 
    11     while(scanf_s("%d", &a, 1) != EOF)
    12     {
    13         if(binary_search(all, all + 10, a))//在all的前十个元素中搜索a
    14             printf_s("Have
    ");
    15         else
    16             printf_s("Not have
    ");
    17     }
    18 
    19     return 0;
    20 }

    3.拷贝函数——copy与copy_backward

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 
     5 ///copy
     6 #include<algorithm>
     7 #include<numeric>
     8 using namespace std;
     9 
    10 class N
    11 {
    12 public:
    13     int a;
    14     int b;
    15     int c;
    16 }all1[100], all2[100];
    17 
    18 int main()
    19 {
    20     all1[0].a = 1;
    21     all1[0].b = 2;
    22     all1[0].c = 3;
    23 
    24     all1[1].a = 4;
    25     all1[1].b = 5;
    26     all1[1].c = 6;
    27 
    28     copy(all1, all1 + 2, all2);//copy_backward
    29 
    30     for(int i = 0 ; i < 2 ; i ++)
    31     {
    32         printf_s("%d	%d	%d
    ", all2[i].a, all2[i].b, all2[i].c);
    33     }
    34 
    35     return 0;
    36 }

    4. 计数——count

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 
     5 //count
     6 #include<algorithm>
     7 #include<numeric>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     int all[11] = {0, 1, 0, 0, 1, 0, 1, 5, 1, 0, 0};
    14 
    15     int num = count(all, all + 8, 5);//数一下all的前八个数里面1的个数
    16     printf_s("%d
    ", num);
    17 
    18     return 0;
    19 }

    5.  条件计数——count_if

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 
     5 //count_if
     6 #include<algorithm>
     7 #include<numeric>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     int all[11] = {0, 1, 2, 3, 4, 5, 6, 4, 5, 6, 10};
    14     int sum = count_if(all, all + 11,bind2nd(equal_to<int>(), 5));//这种方法貌似是被遗弃鸟~~
    15 
    16     printf_s("%d", sum);
    17 
    18     return 0;
    19 }

    6. 相等——equal

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 
     5     //equal
     6 #include<algorithm>
     7 #include<numeric>
     8 
     9     using namespace std;
    10 
    11 int main()
    12 {
    13     int all[11]={0,1,2,3,4,5,6,7,8,9,10};
    14     int all2[11]={0,1,2,3,4,5,6,7,8,9,11};
    15     if(equal(all, all + 10, all2))//比较all与all2的前10个元素是否相等,可以扩展到对象是否相等
    16         printf_s("equal
    ");
    17     else
    18         printf_s("not equal
    ");
    19 
    20     return 0;
    21 }

    7. 填充——fill,fill_n

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    
    //fill,fill_n
    #include<algorithm>
    #include<numeric>
    
    using namespace std;
    
    int main()
    {
        int all[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        //fill(all, all + 11 , 100);//将all的前11个元素填充成100
        fill_n(all, 5, 100);//前5个元素填充成100,其他不变
        printf_s("%d
    ", all[4]);
        printf_s("%d
    ", all[5]);
    }

    8. 查找——find,find_end,find_first_of,find_if

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 
     5 //find,find_end,find_first_of,find_if
     6 #include<algorithm>
     7 #include<numeric>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     int all[14]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6};
    14     int *a = find(all, all + 11, 8);//在all的前11个元素中查找8,如果存在,则返回8所对应的指针
    15     printf_s("%d
    ", *a);
    16 
    17     int all2[4] = {4, 5, 6};
    18     a = find_end(all, all + 14, all2 + 0 , all2 + 3);//找到字串all2在all中最后出现的初始位置
    19     printf_s("位置:%d
    ", a - all);
    20 
    21     a = find_first_of(all, all + 14, all2 + 0 , all2 + 3);//找到字串all2在all中第一次出现的初始位置
    22     printf_s("位置:%d
    ", a - all);
    23 
    24 
    25     int *sum=find_if(all,all+10,bind2nd(greater_equal<int> (), 5));//这种方法被遗弃了
    26     printf("%d",sum-all);
    27 
    28     return 0;
    29 }
  • 相关阅读:
    mybatis_7分页查询
    mybatis_6日志工厂
    mybatis_5解决属性名和字段名不一致的问题(resultMap)
    mybatis_4配置解析
    mybatis_3CRUD操作
    ARM C函数调用堆栈入栈顺序
    syscall SYSCALL_DEFINE*()实现
    ko kallsyms
    elf文件结构解析
    ko module加载flow
  • 原文地址:https://www.cnblogs.com/yiyi-xuechen/p/3452271.html
Copyright © 2011-2022 走看看