您的位置:首页 > 移动开发 > Objective-C

排序及函数对象的基本用法

2014-08-03 13:06 344 查看
1.bool operator()(para...)

2.sort(para1,para2,para3)

#include<iostream>
#include<algorithm>
using namespace std;

class Comp
{
bool flag;
public:
Comp():flag(true){}
Comp(bool in_flag):flag(in_flag){}
bool operator()(const int &a,const int &b)
{
if(flag) return a>b;
return a<b;
}
};

int main(int argc,char **argv)
{
int const SIZE=100;
int *a=new int[SIZE];
int *b=a;
while(cin>>*b)
{
++b;
}
sort(a,b,Comp());
for(int *c=a;c!=b;++c)
{
cout<<*c<<' ';
}
cout<<endl;
sort(a,b,Comp(false));
for(int *c=a;c!=b;++c)
{
cout<<*c<<' ';
}
cout<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ sort function object