商业网站建站目的,公司做网站需要哪些内容,西宁市建设局官方网站,自己设计室内装修软件STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象#xff0c;逻辑运算类仿函数。这些仿函数所产生的对象#xff0c;用法和一般函数完全相同#xff0c;当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象#xff0c;需要引入头文件 functi…STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象逻辑运算类仿函数。这些仿函数所产生的对象用法和一般函数完全相同当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象需要引入头文件 functional
#define _CRT_SECURE_NO_WARNINGS
#includeiostream
using namespace std;
//内建函数对象头文件
#include functional
#include vector
#include algorithmvoid test01()
{//templateclass T T negateT//取反仿函数negateintn;cout n(10) endl;//加法 templateclass T T plusT//加法仿函数plusint p;cout p(1, 1) endl;
}//templateclass T bool greaterT//大于void test02()
{vectorintv;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);sort(v.begin(), v.end(), greaterint());for_each(v.begin(), v.end(), [](int val){ cout val ; });
}int main(){//test01();test02();system(pause);return EXIT_SUCCESS;
}-------分割线-------- sort 排序第三个参数可以是函数名也可以是函数对象
#include string
#include iostream
#include vector
#include algorithm
using namespace std;bool myparse(int v1, int v2) {return v1 v2;
}
class MyParse {
public:bool operator() (int v1, int v2) {return v1 v2;}
};
void test1() {vectorint v1;v1.push_back(20);v1.push_back(35);v1.push_back(5);for (vectorint::iterator it v1.begin(); it ! v1.end(); it) {cout *it endl;}sort(v1.begin(), v1.end());cout -- endl;for (vectorint::iterator it v1.begin(); it ! v1.end(); it) {cout *it endl;}/*sort(v1.begin(), v1.end(), myparse); // 通过函数可以实现自定义排序cout -- endl;for (vectorint::iterator it v1.begin(); it ! v1.end(); it) {cout *it endl;}*/sort(v1.begin(), v1.end(), MyParse()); // 通过函数对象 也可以实现自定义排序cout -- endl;for (vectorint::iterator it v1.begin(); it ! v1.end(); it) {cout *it endl;}
}
int main()
{test1();return 0;
}