泛型算法sort,fill,fill—n,stable—sort,accumulate
2015-07-23 09:15
274 查看
#include <iostream> #include <vector> #include<string> #include<algorithm> #include<numeric> using namespace std; bool abdede(const int &i,const int &j) { return i>j; } int main() { vector<int> a; a.push_back(2); a.push_back(1); a.push_back(3); a.push_back(2); a.push_back(4); a.push_back(3); //搜索到具体的内容,只能采用到顺序搜索 vector<int>::iterator i=find(a.begin(),a.end(),1); //代替了for循环 if(i==a.end()) { cout<<"没有发现"<<endl; return 0; } cout<<*i<<endl; sort(a.begin(),a.end());//从小到大排序, for(int j=0;j<a.size();j++) cout<<a[j]<<endl; a.erase(unique(a.begin(),a.end()),a.end()); stable_sort(a.begin(),a.end(),abdede);//从大到小排序, for( j=0;j<a.size();j++) cout<<a[j]<<endl; reverse(a.begin(),a.end());//倒序 for( j=0;j<a.size();j++) cout<<a[j]<<endl; fill(a.begin(),a.end(),4);//把每一个都填4 int sum=accumulate(a.begin(),a.end(),10);//sum=所以和 cout<<sum<<endl; vector<int> v; fill_n(back_inserter(v),10,1); for( j=0;j<v.size();j++) cout<<v[j]<<endl; return 0; }
相关文章推荐
- ORACLE 定时执行存储过程<一>
- ORACLE 12C新特性——CDB与PDB
- 源码推荐(7.23):FMDBModel使你的实体类具备直接操作数据库的功能,Switch 开关动画效果
- Gson中fromJson方法的返回值
- hdu 2492 Ping pong 树状数组 求逆序数
- Codeforces Round #313 D. Equivalent Strings(DFS)
- 关于SAP---SAP新人或是准备进入SAP领域的新人必看!
- POJ2692假币
- 0037 fgets const
- Hex文件
- 计算机视觉顶级期刊
- caffe使用MemoryDataLayer从内存中加载数据
- UVA_10006_CarmichaelNumbers
- linux 同步IO: sync、fsync与fdatasync、sys_sync【转】
- Linux的WDT(watchdog)驱动
- STM32F4 定时器TIM1输出七路PWM信号【使用库函数
- Eclipse下buildpath和导入jar包问题
- J哥---------Android 多线程下载 仿下载助手
- Python watchdog
- 检测局域网中未使用的IP地址