您的位置:首页 > 编程语言 > C语言/C++

标准库定义的函数对象

2017-04-08 12:01 253 查看
//标准库定义的函数对象

#include<iostream>

#include<functional>

using namespace std;

int main()

{
//算术
//加法 plus<Type>
plus<int> a;
int sum = a(10,20);
cout << "Plus is "<< sum << endl;
//减法 minus<Type>
minus<int> b;
int minus = b(10, 20);
cout << "Minus is " << minus << endl;
//乘法 multiplies<Type>
multiplies<int> c;
int multiplies = c(10, 20);
cout << "Multiplies is " << multiplies << endl;
//除法 divides<Type>
divides<int> d;
int divides = d(10, 10);
cout << "Divides is " << divides << endl;
//取余 modulus<Type>
modulus<int> e;
int modulus = e(30, 30);
cout << "Modulus is " << modulus << endl;
//取反 negate<Type>
negate<int> f;
int negate = f(20);
cout << "Negate is " << negate << endl;
//关系
//相等 equal_to<Type>
equal_to<int> g;
if (g(20, 20))
cout << "It's Equal" << endl;
//不相等 not_equal_to<Type>
not_equal_to<int> j;
if (j(20, 10))
cout << "It's Not Equal" << endl;
//大于 greater<Type>
greater<int> k;
if (k(20, 10))
cout << "The First More than Second" << endl;
//大于等于 greater_equal<Type>
greater_equal<int> h;
if (h(20, 20))
cout << "The First is More than and equal to Second" << endl;
//小于 less<Type>
less<int> m;
if (m(10, 20))
cout << "The First is less than Second" << endl;
//小于等于 les_equal<Type>
less_equal<int> n;
if (n(20, 20))
cout << "The First is less than and equal to Second" << endl;
//逻辑
//且 logical_and<Type>
logical_and<int> v;
cout << "m ,n and is " << v(m(10, 20), n(20, 30)) << endl;
//或 logical_or<Type>
logical_or<int> l;
cout << "m ,n or is " << l(m(10, 20), n(20, 30)) << endl;
//非 loogical_not<Type>
logical_not<int> w;
cout << "m ,n not is " << w(l(m(10, 20), n(20, 30))) << endl;
system("pause");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息