栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 提高篇 7 之 STL 函数对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 提高篇 7 之 STL 函数对象

C++ 自学
  • 函数对象
  • —— 函数对象的使用
  • 谓词
  • —— 一元谓词
  • —— 二元谓词
  • 内建函数对象
  • —— 算术仿函数
  • —— 关系仿函数
  • —— 逻辑仿函数

函数对象

概念

  1. 重载函数调用操作符的类,其对象常称为函数对象
  2. 函数对象使用重载的 () 时,行为类似函数调用,也叫仿函数

本质

函数对象(仿函数)是一个类,不是一个函数

—— 函数对象的使用

特点

  1. 函数对象在使用时,可以像普通函数那样调用,可以有参数,也可以有返回值
#include 
using namespace std;

class addTwoNum {
public:
	int operator()(int num1, int num2) {
		cout << "二数相加结果为:";
		return num1 + num2;
	}
};

int main() {

	// 1. 函数对象在调用时可以有参数和返回值
	addTwonum add; // 创建函数对象
	cout << add(1, 3) << endl; // 使用函数对象
	// outputs:二数相加结果为:4

	system("pause");
	return 0;
}
  1. 函数对象超出普通函数的概念,函数对象可以有自己的状态
#include 
using namespace std;

class addTwoNum {
public:
	addTwoNum() {
		this->index = 0;
	}
	int operator()(int num1, int num2) {
		index++;
		return num1 + num2;
	}

	int index;
};

int main() {

	// 2. 函数对象也可以有自己的状态
	addTwonum add; // 创建函数对象
	// 使用函数对象 -- 并调用三次
	add(1, 4);
	add(1, 7);
	add(1, 8);
	// 利用函数对象自己的状态打印出被调用的次数
	cout << "add 调用次数:" << add.index << endl; // add 调用次数:3

	system("pause");
	return 0;
}
  1. 函数对象可以作为参数传递
#include 
using namespace std;

class addTwoNum {
public:
	int operator()(int num1, int num2) {
		return num1 + num2;
	}
};

// 函数
void Add(addTwonum &add, int num1, int num2) {
	cout << "二数相加结果为:" << add(num1, num2) << endl;
}

int main() {

	// 3. 函数对象可以作为参数传递
	addTwonum add; // 创建函数对象
	Add(add, 1, 3); // 二数相加结果为:4

	system("pause");
	return 0;
}
谓词

概念

  1. 返回 bool 类型的仿函数称为谓词
  2. 如果 operator() 接收一个参数,称为一元谓词
  3. 如果 operator() 接收二个参数,称为二元谓词
—— 一元谓词
#include 
using namespace std;
#include 
#include 

class greaterFive {
public:
	// 一元谓词
	bool operator()(int num) {
		return num > 5; // 大于 5 则返回 true
	}
};

int main() {

	vector v;
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);
	v.push_back(1);

	// 查找容器中有没有大于 5 的数
	// 利用算法 find_if -- 返回迭代器指向位置 pos (目前不要求掌握)
	// greaterFive() 为匿名函数对象
	vector::iterator pos = find_if(v.begin(), v.end(), greaterFive());
	if (pos != v.end()) {
		cout << "存在大于 5 的数:" << *pos << endl; // 存在大于 5 的数:7
	}
	else {
		cout << "不存在大于 5 的数" << endl;
	}


	system("pause");
	return 0;
}
—— 二元谓词
#include 
using namespace std;
#include 
#include 

class MyCompare {
public:
	// 二元谓词 -- 降序
	bool operator()(int num1, int num2) {
		return num1 > num2;
	}
};

void PrintVector(vector &v) {
	for (vector::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	vector v;
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);
	v.push_back(1);

	// 排序之前
	PrintVector(v); // 5 7 9 1

	// 算法 sort 将 v 进行排序(默认升序)
	sort(v.begin(), v.end(), MyCompare());
	
	// 排序之后
	PrintVector(v); // 9 7 5 1

	system("pause");
	return 0;
}
内建函数对象

概念

STL 内建了一些函数对象

分类

  1. 算术仿函数
  2. 关系仿函数
  3. 逻辑仿函数

用法

  1. 这些仿函数所产生的对象,用法和一般函数完全相同
  2. 使用内建函数对象,需要引入头文件 #include
—— 算术仿函数

功能描述

  1. 实现四则运算
  2. 其中 negate 是一元运算,其它都是二元运算

仿函数原型

  1. template T plus // 加法仿函数
  2. template T minus // 减法仿函数
  3. template T multilies // 乘法仿函数
  4. template T divides // 除法仿函数
  5. template T modulus // 取模仿函数
  6. template T negate // 取反仿函数
#include 
using namespace std;
#include 

int main() {

	// 1. 一元运算
	negate n;
	cout << n(100) << endl; // -100

	// 2. 二元运算
	// 加法
	plus p;
	cout << p(1, 5) << endl; // 6
	// 减法
	minus m;
	cout << m(5, 6) << endl; // -1
	// 乘法
	multiplies ml;
	cout << ml(5, 7) << endl; // 35
	// 除法
	divides d;
	cout << d(9, 4) << endl; // 2
	// 取模
	modulus md;
	cout << md(5, 4) << endl; // 1

	system("pause");
	return 0;
}
—— 关系仿函数

功能描述

实现关系对比

仿函数原型

  1. template bool equal_to // 等于
  2. template bool not_equal // 不等于
  3. template bool greater // 大于
  4. template bool greater_equal // 大于等于
  5. template bool less // 小于
  6. template bool less_equal // 小于等于
#include 
using namespace std;
#include 
#include 
#include 

int main() {

	// 1. 是否相等
	equal_to e;
	if (e(1, 1)) {
		cout << "二数相等!" << endl; // 二数相等!
	}

	// 2. 用于排序
	vector v;
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);
	v.push_back(1);
	// 用仿函数实现降序排列
	sort(v.begin(), v.end(), greater());
	// sort(v.begin(), v.end(), MyCompare()); 
	for (vector::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " "; // 9 7 5 1
	}
	cout << endl; 

	system("pause");
	return 0;
}
—— 逻辑仿函数

功能描述

实现逻辑运算

仿函数原型

  1. template bool logical_and // 逻辑与
  2. template bool logical_or // 逻辑或
  3. template bool logical_not // 逻辑非
#include 
using namespace std;
#include 
#include 
#include 

void PrintVector(vector &v) {
	for (vector::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " "; 
	}
	cout << endl;
}

int main() {

	vector v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(false);
	v.push_back(true);

	// 1. 逻辑与
	logical_and l_a;
	if (l_a(1, 1)) {
		cout << "二数的逻辑与运算为 true!" << endl; // 二数的逻辑与运算为 true!
	}

	PrintVector(v); // 1 0 0 1

	// 2. 利用逻辑非,将容器 v 搬运到容器 v1 中,并执行取反操作
	vector v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(), v1.begin(), logical_not());
	PrintVector(v1); // 0 1 1 0

	system("pause");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号