C++ greater()和less()

1.greater和less是头文件

template  struct greater {
  bool operator() (const T& x, const T& y) const {return x>y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

template  struct less {
  bool operator() (const T& x, const T& y) const {return x



2.在sort()函数中使用greater<>()和less()

#include
#include
#include
#include
#include
using namespace std;

int main()
{
	int A[]={1,4,3,7,10};
	const int N=sizeof(A)/sizeof(int);
	vector vec(A,A+N);
	ostream_iterator output(cout," ");
	cout<<"Vector vec contains:";
	copy(vec.begin(),vec.end(),output);
	
	cout<<"\nAfter greater():";
	sort(vec.begin(),vec.end(),greater());//内置类型从大到小 
	copy(vec.begin(),vec.end(),output);
	
	cout<<"\nAfter less():";
	sort(vec.begin(),vec.end(),less());   //内置类型小大到大 
	copy(vec.begin(),vec.end(),output);
	
	return 0;
}

C++ greater()和less()_第1张图片







你可能感兴趣的:(C/C++学习)