4、STL-函数对象

4、STL-函数对象

4.1函数对象

4.1.1 函数对象概念

概念:

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

本质:

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

4.1.2 函数对象的使用

特点:

  • 函数对象在使用时,可以像普通函数那样,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include<iostream>
#include<string>
using namespace std;


class MyAdd {
public:
	int operator()(const int a, const int b) {
		return a + b;
	}
};

class myPrint {
public:
	myPrint() {
		count = 0;
	}
	void operator()(string test) {
		cout << test << endl;
		count++;
	}

	int count;
};

class doPrint {
public:
	void operator()(myPrint& myprint, string test) {
		myprint(test);
	}
};

void test() {
	// 函数对象可以像函数一样有参数,有返回值
	int a = 10;
	int b = 20;
	MyAdd add;
	int sum = add(a, b);
	cout << sum << endl;
	// 函数对象可以有自己的状态
	myPrint myprint;
	myprint("sunwukong");
	cout << myprint.count << endl;
	
	// 函数对象可以当作参数被调用
	doPrint doprint;
	doprint(myprint	, "zhubajie");
	cout << myprint.count << endl;

}

int main() {
	test();
	system("pause");
	return 0;
}

4.2 谓词

4.2.1 谓词概念

概念

  • 返回bool类型的仿函数称为谓词
  • 如果operator()接受一个参数, 那么叫做一元谓词
  • 如果接受两个参数, 那么叫做二元谓词

4.2.2一元谓词

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

class GreateFive {
public:
	bool operator()(int val) {
		return val > 5;
	}
};


void test() {
	vector<int> v;
	v.push_back(1);
	v.push_back(10);
	v.push_back(5);

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreateFive());
	if (it == v.end()) {
		cout << "未找到" << endl;
	}
	else {
		cout << *it << endl;
	}
}

int main() {
	test();
	system("pause");
	return 0;
}

4.2.3二元谓词

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>


class Mycompare {
public:
	bool operator()(int val1, int val2) {
		return val1 > val2;
	}
};


void test() {
	vector<int> v;
	v.push_back(1);
	v.push_back(10);
	v.push_back(5);

	sort(v.begin(), v.end(), Mycompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it<<"   ";
	}
	cout << endl;
}

int main() {
	test();
	system("pause");
	return 0;
}

4.3内建函数对象

4.3.1内建函数对象意义

概念:

  • STL内建了一些函数对象

分类:

  • 算数仿函数
  • 关系仿函数
  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建函数对象,需要引入头文件 #include<functional>

4.3.2算数仿函数

功能描述:

  • 实现四则运算
  • 其中negate是一元运算,其他都是二元运算

仿函数原型:

  • template T plus // 加法仿函数 第一个T代表返回值类型,第二个表示,传入类中也就是仿函数中的数据类型 如int
  • template T minus // 减法仿函数
  • templateT multiplies // 乘法仿函数
  • template T divides // 除法仿函数
  • templateT modulus // 取模仿函数
  • template T negate // 取反仿函数

示例:

#include<iostream>
#include<functional>
using namespace std;

#include<string>

// negate是一元仿函数,
void test() {
	negate<int> n;
	cout << n(10) << endl;  // -10

	// 加法 plus
	plus<int> p;
	cout << p(10, 20) << endl;  // 30

	// 减法 minus
	minus<int> m;
	cout << m(20, 10) << endl;    //10

	// 乘法  multiplies
	multiplies<float> mul;
	cout << mul(10.0f, 2.5f) << endl;  //25

	// 除法 divides
	divides<float> div;
	cout << div(10.0f, 2.5f) << endl;  //4

	// 取模仿函数 modulus  10%3
	modulus<int> mod;
	cout << mod(10, 3) << endl;   // 1
}

int main() {
	test();
	system("pause");
	return 0;
}

4.3.3 关系仿函数

功能描述:

  • 实现关系对比

仿函数原型:

  • template bool equal_to // 等于
  • template bool not_equal_to // 不等于
  • template bool greater // 大于
  • template bool greater_equal // 大于等于
  • template bool less // 小于
  • template bool less_equal // 小于等于
#include<iostream>
#include<string>
using namespace std;
#include<algorithm>
#include<functional>
#include<vector>	



void test() {
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(30);
	
	sort(v.begin(), v.end(),greater<int>());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << "  ";
	}
	cout << endl;
}

int main() {
	test();
	system("pause");
	return 0;
}

4.3.4 逻辑仿函数

功能描述:

  • 实现逻辑运算

函数原型:

  • templatebool logical_and // 逻辑与
  • template bool logical_or //逻辑或
  • templatebool logical_not // 逻辑非
#include<iostream>
#include<string>
using namespace std;
#include<algorithm>
#include<functional>
#include<vector>	



void test() {
	vector<bool> v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(false);
	v.push_back(true);

	for (vector< bool >::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << "  ";
	}
	cout << endl;
	vector<bool> v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());
	for (vector< bool >::iterator it = v1.begin(); it != v1.end(); it++) {
		cout << *it << "  ";
	}
	cout << endl;
}

int main() {
	test();
	system("pause");
	return 0;
}

5、STL-常用算法

概述:

  • 算法主要是由头文件 <algorithm> <functional> <numeric> 组成
  • 是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等
  • 体积很小,只包括几个在序列上面进行简单数学运算的模板函数
  • 定义了一些模板类,用以声明函数对象

5.1常用遍历算法

学习目标:

  • 掌握常用的遍历算法

算法简介:

  • for_each // 遍历容器
  • transform // 搬运容器到另一个容器中

5.1.1 for_each

功能描述:

  • 实现遍历容器

函数原型:

  • for_each(iterator beg, iterator end, _func);

    //遍历算法,遍历容器元素

    // beg 开始迭代器

    // end 结束迭代器

    // _func 函数或者函数对象

示例:

总结:for_each在实际开发中是最常用的遍历算法,需要熟练掌握

5.1.2 transform

功能描述:

  • 搬运容器到另一个容器中

函数原型:

  • transform(iterator beg1, iterator end1, iterator beg2, func);

beg1 源容器开始迭代器

end1 源容器结束迭代器

beg2 目标容器开始迭代器

func 函数或者函数对象, 在搬运过程中对搬运对象实现的中间操作

示例:

总结:搬运的目标容器必须要提前开辟空间,否则无法正常搬运

5.2常用查找算法

算法简介:

  • find 查找元素
  • find_if 按条件查找元素
  • adjacent_find 查找相邻重复元素
  • binary_search 二分查找法
  • count 统计元素个数
  • count_if 按条件统计元素个数

5.2.1 find

功能描述:

  • 查找指定元素, 找到返回指定元素的迭代器,找不到返回结束迭代器end()

函数原型:

  • find(iterator beg, iterator end, value);

按值查找元素, 找到返回指定位置迭代器,找不到返回结束迭代器位置

beg 开始迭代器

end 结束迭代器

value 查找的元素

find对比数据类型是通过== , 想要查找自定义数据类型, 需要对==进行重载,返回bool类型数据

示例:

5.2.2 find_if

功能描述:

  • 按照条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred);

beg 开始迭代器

end 结束迭代器

_Pred 函数或者谓词(返回bool类型的仿函数)

5.2.3 adjacent_find

功能描述:

  • 查找相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);

查找相邻重复元素, 返回相邻元素的第一个位置的迭代器

beg 开始迭代器

end 结束迭代器

示例:

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, value);

查找指定元素, 查到返回true, 否则false

注意:在无序序列中不可用

beg 开始迭代器

end 结束迭代器

value 查找的元素

示例:

总结:二分查找法查找的效率很高,值得注意的是查找的容器中元素必须是有序序列

5.2.5 count

功能描述:

  • 统计元素个数

函数原型:

  • count(iterator beg, iterator end, value);

beg 开始迭代器

end 结束迭代器

value 统计的元素

5.2.6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);

_Pred 谓词

5.3 常用排序算法

posted @ 2022-10-03 16:50  小白就是我_fxm  阅读(25)  评论(0)    收藏  举报