#include
#include
#include
using namespace std;
void output(int &s) //输出函数
{
cout<<s<<endl;
}
bool mycomp(const int &s1,const int &s2)
{
return s1>s2;
}
int main(int argc,char * argv[])
{
vector myvt;
myvt.insert(myvt.begin(),2);
myvt.insert(myvt.begin()+1,4);
myvt.insert(myvt.end(),1);
cout<<"原顺序 :"< for_each(myvt.begin(),myvt.end(),output);
cout<<"升序:"< sort(myvt.begin(),myvt.end()); //默认升序排列
for_each(myvt.begin(),myvt.end(),output);
cout<<"降序:"< sort(myvt.begin(),myvt.end(),mycomp);
for_each(myvt.begin(),myvt.end(),output);
}
本文详细介绍了在C++编程中利用STL(标准模板库)进行数据排序与输出的方法,包括使用insert函数插入元素,sort函数进行升序与降序排列,并通过自定义比较函数实现特定排序需求。
1736

被折叠的 条评论
为什么被折叠?



