greater和less是头文件<functional>
2018-03-27 10:30
1871 查看
1.greater和less是头文件<functional>[cpp] view plain copytemplate <class T> 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;
};
[cpp] view plain copytemplate <class T> struct less {
bool operator() (
e03a
const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
2.在sort()函数中使用greater<>()和less<int>()[cpp] view plain copy<span style="font-size:12px;">#include<iostream>
#include<vector>
#include<iterator>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
int A[]={1,4,3,7,10};
const int N=sizeof(A)/sizeof(int);
vector<int> vec(A,A+N);
ostream_iterator<int> output(cout," ");
cout<<"Vector vec contains:";
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter greater<int>():";
sort(vec.begin(),vec.end(),greater<int>());//内置类型从大到小
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter less<int>():";
sort(vec.begin(),vec.end(),less<int>()); //内置类型小大到大
copy(vec.begin(),vec.end(),output);
return 0;
}</span>
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;
};
[cpp] view plain copytemplate <class T> struct less {
bool operator() (
e03a
const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
2.在sort()函数中使用greater<>()和less<int>()[cpp] view plain copy<span style="font-size:12px;">#include<iostream>
#include<vector>
#include<iterator>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
int A[]={1,4,3,7,10};
const int N=sizeof(A)/sizeof(int);
vector<int> vec(A,A+N);
ostream_iterator<int> output(cout," ");
cout<<"Vector vec contains:";
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter greater<int>():";
sort(vec.begin(),vec.end(),greater<int>());//内置类型从大到小
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter less<int>():";
sort(vec.begin(),vec.end(),less<int>()); //内置类型小大到大
copy(vec.begin(),vec.end(),output);
return 0;
}</span>
相关文章推荐
- 关于STL中的greater<T>()和less<T>()
- POJ 3253 Fence Repair (priority_queue的用法) ----- less<int> 和 greater<int>
- greater<int>()和less<int>()的使用
- greater<int>()表示内置类型从大到小排序,less<int>()表示内置类型从小到大排序
- greater<int>()和less<int>()的使用
- greater<int>()和less<int>()的使用
- greater<int>()和less<int>()的使用
- C++ greater<int>()和less<int>()
- STL greater<T>() 和less<T>()
- <poj 1046>Color Me Less
- 包含所有C++头文件的头文件 <bits/stdc++.h>
- 关于C++中头文件<climits>
- nios中的头文件上的#include<>和#include""的区别
- c语言:常用各种头文件的作用,如:#include <stdlib.h>
- ios调支付宝找不到头文件<openssl/rsa.h>
- 万能的头文件 #include <bits/stdc++.h>
- libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件
- c语言的头文件#include <limits.h>
- ios调支付宝找不到头文件<openssl/rsa.h>
- 万能头文件#include <bits/stdc++.h>