排序及函数对象的基本用法
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;
}
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;
}
相关文章推荐
- java中Collections.sort() 排序函数针对对象的用法
- JavaScript对象数组排序函数及六个用法
- ROW_NUMBER() OVER 排序函数的基本用法
- JavaScript对象数组排序函数及六个用法
- 函数的实参形参,arguments对象基本用法
- Bitblt函数的基本用法
- SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
- C++ 排序函数 sort(),qsort()的用法【转】
- Qsort和Sort排序函数用法
- Brew 基本函数用法
- java 中Collections.sort() 排序函数的用法
- stl基本容器学习——函数对象
- java中Collections.sort() 排序函数的用法
- 简单函数对象取代默认排序准则,改变默认sort()的行为
- ROW_NUMBER() OVER函数的基本用法用法
- Javascript中对象的基本用法
- SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
- 基本函数的用法,记录以备后用
- php引用(&)变量引用,函数引用,对象引用和参数引用用法详解
- 系统调用-进程创建函数基本用法1