/*STL 内建了一些仿函数 内建函数对象
【分类】
1-算数仿函数
2-关系仿函数
3-逻辑仿函数
【算数仿函数】
template<class T> T plus<T> //加法仿函数
template<class T> T minus<T> //减法仿函数
template<class T> T multiplies<T> //乘法仿函数
template<class T> T divides<T> //除法仿函数
template<class T> T modulus<T> //取模仿函数
template<class T> T negate<T> //取反仿函数
...
【关系仿函数】
template<class T> bool equal_to<T> //等于
template<class T> bool not_equal_to<T> //不等于
template<class T> bool greater<T> //大于
template<class T> bool greater_equal<T> //大于等于
template<class T> bool less<T> //小于
template<class T> bool less_equal<T> //小于等于
【逻辑仿函数】
template<class T> bool logical_and<T> //与
template<class T> bool logical_or<T> //或
template<class T> bool logical_not<T> //非
*/
#include<iostream>
#include<string>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
class MyCompare;
class MyCompare
{
public:
bool operator()(int a,int b)
{
return a>b;
}
};
int main()
{
cout << "------------------------- 算数仿函数" << endl;
//整型取反
negate<int> n;
cout << n(34) << endl;
//加法
plus<int> m;
cout << m(123,123) <<endl;
cout << "\n\n------------------------- 关系仿函数" << endl;
vector<int> v;
v.push_back(13);
v.push_back(23);
v.push_back(53);
v.push_back(33);
v.push_back(1);
for (vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
//降序
//sort(v.begin(),v.end(),MyCompare());
sort(v.begin(),v.end(),greater<int>());
for (vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
cout << "\n\n------------------------- 逻辑仿函数" << endl;
vector<bool> v2;
v2.push_back(true);
v2.push_back(true);
v2.push_back(true);
v2.push_back(false);
for (vector<bool>::iterator it = v2.begin();it != v2.end();it++)
{
cout << *it << " ";
}
cout << endl;
//利用逻辑非将容器v2复制到->v3
vector<bool> v3;
v3.resize(v2.size());
transform(v2.begin(),v2.end(),v3.begin(),logical_not<bool>());//搬运函数
for (vector<bool>::iterator it = v3.begin();it != v3.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
38-【什么叫规矩 什么叫体统】内建函数
猜你喜欢
转载自blog.csdn.net/magic_shuang/article/details/107593593
今日推荐
周排行