C++STL专题二
#include<bits/stdc++.h>
const int inf=0x7fffffff;
using namespace std;
void print(int &m)
{
cout<<m<<" ";
}
int main()
{
vector<int>p;
vector<int>p1;
vector<int>::iterator it,it1,it3;
for(int i=0;i<10;i++)
{
int value=rand()%100+1;
p.push_back(value);
p1.push_back(i);
}
random_shuffle(p.begin (),p.end() );
random_shuffle(p1.begin (),p1.end() );
cout<<"输出p未发生变化前的序列"<<endl;
for(it=p.begin() ;it!=p.end() ;++it)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<"输出p1未发生变化前的序列"<<endl;
for(it1=p1.begin() ;it1!=p1.end() ;++it1)
{
cout<<*it1<<" ";
}
cout<<endl;
cout<<"输出p按照 3 位步长旋转后的序列"<<endl;
rotate(p.begin (),p.begin ()+3,p.end() );
for(it=p.begin() ;it!=p.end() ;++it)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<"输出 p 的各项相加得到的结果"<<endl;
cout<< accumulate(p.begin (),p.end(),0);
cout<<endl;
cout<<"输出 p 删除 k元素后的序列"<<endl;
remove(p.begin (),p.end(),4);
for(it=p.begin() ;it!=p.end() ;it++)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<"输出 p1 相邻两项的差"<<endl;
it1=p1.begin();
adjacent_difference(p1.begin(), p1.end(), it1, minus<int>());
for(it1=p1.begin() ;it1!=p1.end() ;++it1)
{
cout<<*it1<<" ";
}
cout<<endl;
cout<<"输出p前n项和"<<endl;
vector<int>p2;
partial_sum( p.begin(),p.end(),back_inserter(p2));
for(it3=p2.begin() ;it3!=p2.end() ;++it3)
{
cout<<*it3<<" ";
}
cout<<endl<<"序列中的最大、最小元素为:"<<endl;
int max=*max_element(p.begin(),p.end());
int min=*min_element(p.begin(),p.end());
cout<<max<<" "<<min<<endl;
for_each(p.begin(),p.end(),print);
cout<<endl<<"p&p1的内积为:";
cout<<inner_product(p.begin(),p.end(),p1.begin() ,0);
return 0;
}