c++ 内建函数对象

#include <iostream>

#include <string>

#include <fstream>

#include <vector>

#include <algorithm>

#include <deque>

#include <stack>

#include <queue>

#include <list>

#include <set>

#include <map>

#include <functional>

using namespace std;

void test()

{

negate<int> n;

cout << n(10) << endl; // 输出 -10

}

void test2()

{

plus<int> p;

cout << p(10, 20) << endl;

}

class MyCompare

{

public:

bool operator()(int a, int b)const

{

return a > b;

}

};

void test3()

{

vector<int> v;

v.push_back(10);

v.push_back(30);

v.push_back(20);

for(vector<int>::iterator i = v.begin(); i != v.end(); i++)

{

cout << *i << " ";

}

cout<< endl;

//sort(v.begin(), v.end() , MyCompare());

sort(v.begin(), v.end(), greater<int>());

for(vector<int>::iterator i = v.begin(); i != v.end(); i++)

{

cout << *i << " ";

}

}

void test4()

{

vector<bool> v;

v.push_back(true);

v.push_back(false);

v.push_back(true);

for(vector<bool>::iterator i = v.begin(); i != v.end(); i++)

{

cout << *i << " ";

}

cout<< endl;

vector<bool>v2;

v2.resize(v.size());

transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());

for(vector<bool>::iterator i = v2.begin(); i != v2.end(); i++)

{

cout << *i << " ";

}

}

int main()

{

test();

test2();

test3();

test4();

return 0;

system("pause");

}

相关推荐
Kratzdisteln11 小时前
【MVCD 3】
开发语言·php
癫狂的兔子11 小时前
【Python】【NumPy】random.rand和random.uniform的异同点
开发语言·python·numpy
TDengine (老段)11 小时前
TDengine C/C++ 连接器入门指南
大数据·c语言·数据库·c++·物联网·时序数据库·tdengine
自然语11 小时前
人工智能之数字生命-特征类升级20260106
人工智能·算法
菜鸟233号11 小时前
力扣343 整数拆分 java实现
java·数据结构·算法·leetcode
先做个垃圾出来………11 小时前
Python整数存储与位运算
开发语言·python
赫凯11 小时前
【强化学习】第五章 时序差分算法
算法
vyuvyucd11 小时前
C++ vector容器完全指南
c++
liulilittle11 小时前
XDP VNP虚拟以太网关(章节:三)
网络·c++·网络协议·信息与通信·通信·xdp
leiming611 小时前
c++ find_if 算法
开发语言·c++·算法