Algorithm常用函数(1)
2013-12-01 10:39
204 查看
1.求和——accumulate
2.二分查找—— binary_search
3.拷贝函数——copy与copy_backward
4. 计数——count
5. 条件计数——count_if
6. 相等——equal
7. 填充——fill,fill_n
8. 查找——find,find_end,find_first_of,find_if
#include<iostream> #include<cstring> #include<cstdio> #include<numeric>//accumulate using namespace std; int main() { int all[100] = {2,2,3,4,5,6,7,8,9,10}; int sum = accumulate(all, all + 3, 1);//all的前三个数相加,和的初始值是1 printf("%d", sum); return 0; }
2.二分查找—— binary_search
#include<algorithm> #include<numeric> using namespace std; int main() { int all[100] = {0, 1, 2, 3, 4, 5, 89, 104, 300, 600, 9000}; int a; while(scanf_s("%d", &a, 1) != EOF) { if(binary_search(all, all + 10, a))//在all的前十个元素中搜索a printf_s("Have\n"); else printf_s("Not have\n"); } return 0; }
3.拷贝函数——copy与copy_backward
#include<cstdio> #include<iostream> #include<cstring> ///copy #include<algorithm> #include<numeric> using namespace std; class N { public: int a; int b; int c; }all1[100], all2[100]; int main() { all1[0].a = 1; all1[0].b = 2; all1[0].c = 3; all1[1].a = 4; all1[1].b = 5; all1[1].c = 6; copy(all1, all1 + 2, all2);//copy_backward for(int i = 0 ; i < 2 ; i ++) { printf_s("%d\t%d\t%d\n", all2[i].a, all2[i].b, all2[i].c); } return 0; }
4. 计数——count
#include<cstdio> #include<cstring> #include<iostream> //count #include<algorithm> #include<numeric> using namespace std; int main() { int all[11] = {0, 1, 0, 0, 1, 0, 1, 5, 1, 0, 0}; int num = count(all, all + 8, 5);//数一下all的前八个数里面1的个数 printf_s("%d\n", num); return 0; }
5. 条件计数——count_if
#include<cstdio> #include<iostream> #include<cstring> //count_if #include<algorithm> #include<numeric> using namespace std; int main() { int all[11] = {0, 1, 2, 3, 4, 5, 6, 4, 5, 6, 10}; int sum = count_if(all, all + 11,bind2nd(equal_to<int>(), 5));//这种方法貌似是被遗弃鸟~~ printf_s("%d", sum); return 0; }
6. 相等——equal
#include<cstdio> #include<iostream> #include<cstring> //equal #include<algorithm> #include<numeric> using namespace std; int main() { int all[11]={0,1,2,3,4,5,6,7,8,9,10}; int all2[11]={0,1,2,3,4,5,6,7,8,9,11}; if(equal(all, all + 10, all2))//比较all与all2的前10个元素是否相等,可以扩展到对象是否相等 printf_s("equal\n"); else printf_s("not equal\n"); return 0; }
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\n", all[4]); printf_s("%d\n", all[5]); }
8. 查找——find,find_end,find_first_of,find_if
#include<cstdio> #include<cstring> #include<iostream> //find,find_end,find_first_of,find_if #include<algorithm> #include<numeric> using namespace std; int main() { int all[14]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6}; int *a = find(all, all + 11, 8);//在all的前11个元素中查找8,如果存在,则返回8所对应的指针 printf_s("%d\n", *a); int all2[4] = {4, 5, 6}; a = find_end(all, all + 14, all2 + 0 , all2 + 3);//找到字串all2在all中最后出现的初始位置 printf_s("位置:%d\n", a - all); a = find_first_of(all, all + 14, all2 + 0 , all2 + 3);//找到字串all2在all中第一次出现的初始位置 printf_s("位置:%d\n", a - all); int *sum=find_if(all,all+10,bind2nd(greater_equal<int> (), 5));//这种方法被遗弃了 printf("%d",sum-all); return 0; }
相关文章推荐
- 字符串匹配算法之Boyer-Moore-Horspool Algorithm
- 蒙哥马利(Montgomery)算法简介
- Django ModelForm的使用
- Django Form
- Django 自验证
- django如何使ForeignKey字段显示树状结构
- HDU3635Dragon Balls(并查集)
- google probuf反射原理之源码分析
- Rockchip3066 修改开机 LOGO 和开机动画
- google probuf源码分析之descriptor
- 2012-11-22《gotta have you》
- Lesson 15 Good news 佳音
- Lesson 12 Goodbye&nbsp…
- Lesson 11 One good turn deserves…
- Exit 与 Goto :eof 在批处理中的区别
- XTUOJ 1168 填颜色
- 如何在阿里云服务器部署Django
- Algorithm--多直线之间的斜率关系
- ubuntu12.04配置goagent+chrome
- 写一个地道的django model