#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;
}
泛型算法sort,fill,fill—n,stable—sort,accumulate
最新推荐文章于 2021-01-17 20:12:59 发布