C++模板技术和STL实战开发(11)——STL容器与算法(5)——泛型算法通则和非变异算法

1.泛型算法通则

所有算法的前两个参数都是一对iterator:[first,last),用来指出容器内一个范围内的元素

每个算法的声明中,都表现出它所需要的最低层次的iterator类型

大部分算法都可以用仿函数(函数对象)来更改准则

注:Lambda表达式是匿名仿函数

代码示例:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

//仿函数就是重载(),然后通过创建它的对象,调用对象就是会调用operator()
template <class T>
class Add
{
public:
	void operator()(T& t)
	{
		t = t * 2;
		cout << t << endl;
	}
};

int main()
{
	vector<int> myV;
	myV.push_back(10);
	myV.push_back(9);
	myV.push_back(8);
	myV.push_back(7);
	myV.push_back(6);
	
	Add<int> myAdd;
	for_each(myV.begin(),myV.end(),myAdd);

	//lambda是匿名仿函数也可以传入算法的第三个参数
	cout << "-----------------------" << endl;
	auto fun = [](int t) {t = t * 3; cout << t << endl; };
	for_each(myV.begin(), myV.end(), fun);

	//也可直接传入
	cout << "-----------------------" << endl;
	for_each(myV.begin(), myV.end(), [](int t) {t = t * 4; cout << t << endl; });
}

算法都是施加在容器中的

2.非变异算法

(1)for_each

for_each扩展:能够利用一元函数模板技术实现求vector的最大、最小,sum

代码实现:

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

template <typename T,typename outPara>
class PrintInfo:public unary_function<T,outPara>
{
public:
	PrintInfo():count(0),sum(0){}
	T getSum()
	{
		return sum;
	}

	T getMax()
	{
		return max;
	}
	T getMin()
	{
		return min;
	}

	outPara operator()(T t)
	{
		if (count == 0)
		{
			max = t;
			min = t;
		}
		else
		{
			if (max < t)
				max = t;
			if (min > t)
				min = t;
		}
		sum += t;
		count++;
	}
private:
	T sum;
	T max;
	T min;
	int count;
};

int main()
{
	float A[] = { 1.0,2.1,3.2,4.3,5.4 };
	const int N = sizeof(A)/sizeof(float);

	//仿函数:实际上完成了容器元素的回调函数
	PrintInfo<float, void> p = for_each(A,A+N,PrintInfo<float,void>());

	cout << "sum = " << p.getSum() << endl;
	cout << "max = " << p.getMax() << endl;
	cout << "min = " << p.getMin() << endl;
}

(2)各种find函数示例

代码示例:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool myGreater(int temp)
{
	return temp > 4;
}

int main()
{
	int a[] = {1,1,2,2,3,4,4,5,6,7,1,2,2,3};
	int size_a = sizeof(a) / sizeof(int);
	//1.第一个等于3的位置
	int *p1 = find(a, a + size_a, 3);
	if (p1 != a + size_a)
		cout << p1 - a << endl;
	//2.出现第一个大于4的位置
	int *p2 = find_if(a, a + size_a, myGreater);
	if (p2 != a + size_a)
		cout << p2 - a << endl;

	int b[] = { 16,6,15 };
	int size_b = sizeof(b) / sizeof(int);
	//3.找到a中第一个在b中出现的元素位置(a元素的位置)
	int *p3 = find_first_of(a, a + size_a, b, b+size_b);
	if(p3 != a+size_a)
		cout << p3 - a<<"\t"<<*p3 << endl;
	//4.首次相邻元素的位置
	int *p4 = adjacent_find(a,a+size_a);
	if (p4 != a + size_a)
		cout << p4 - a << "\t" << *p4 << endl;

	int c[] = { 2,3 };
	int size_c = sizeof(c) / sizeof(int);
	//5.最后一次匹配c数组的位置
	int *p5 = find_end(a, a + size_a, c, c + size_c);
	if (p5 != a + size_a)
		cout << p5 - a << endl;
	//6.首次匹配c数组的位置
	int *p6 = search(a, a + size_a, c, c + size_c);
	if (p6 != a + size_a)
		cout << p6 - a << endl;
}

(3)count的使用

代码示例:

#include <iostream>
#include <string>
#include <algorithm>
#include <array>
#include <bitset>

using namespace std;

int main()
{
	bitset<7> b(string("1101000"));
	array<int,7>  arr;
	for (int i = 0; i < 7; i++)
		arr[i] = b[i];
	cout << "1的总数:" <<count(arr.begin(),arr.end(),1)<< endl;
}

(4)mismatch

代码示例:

#include <iostream>
#include <algorithm>


using namespace std;

//寻找两个整型数组中元素不相等时候的元素值
int main()
{
	int a1[] = {3,1,4,1,5,9,3};
	int a2[] = { 3,1,4,2,8,5,7 };
	const int size_a1 = sizeof(a1) / sizeof(int);

	//pair是高度抽象的二元组类模板
	//其实mismatch中是判断*(a1+i)==*(a2+i)    等于的话:i++    不等于的话:返回pair对象
	pair<int*, int*> res = mismatch(a1, a1 + size_a1, a2);

	cout << "首次出现不一致的位置" << res.first - a1 << endl;

	cout << "对应的数值" << *(res.first) << "," << *(res.second) << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值