C++语法学习笔记七十五:适配器概念、分类、范例及总结

实例代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
#include <unordered_set>
#include <functional>
#include <queue>

using namespace std;
//
namespace _nmsp1
{
	//一:适配器基本概念:转接头
	//把一个既有的东西 进行适当的改造,比如增加点东西,或者减少点东西,就构成了一个适配器;
	//三种适配器:容器适配器,算法适配器,迭代器适配器。。。。

	//二:容器适配器(类模板):本章三节学过双端队列deque;
	//(2.1)stack:堆栈,是属于阉割版的deque;
	//(2.2)queue:队列,是属于阉割版的deque;
	//。。。。。。

	//三:算法适配器(函数适配器) :最典型的就是绑定器(binder)
	//(3.1)绑定器
	//老版本 bind1st,bind2nd;c++11,名字被修改为bind:未归类知识点第七节
	//https://en.cppreference.com/w/
	//http://www.cplusplus.com/

	class A
	{
	public:
		bool operator()(int i)
		{
			//return i > 40; //希望大于40的元素被统计
			return 40 < i;  //希望大于40的元素被统计
		}
	};

	void func()
	{
		vector<int> myvector = { 50, 15, 80, 30, 46, 80 };
		//统计某个值出现的次数
		int cishu = count(myvector.begin(), myvector.end(), 80); //算法
		cout << cishu << endl;

		//A myobja;
		//cishu = count_if(myvector.begin(), myvector.end(), myobja);
		//cout << cishu << endl;

		//bind(less<int>(), 40, placeholders::_1);
		//less<int>的operator()的第一个参数绑定为40,那这样当调用less<int>()这个可调用对象时,
		//第二个参数,就用这里的 placeholders::_1表示,在调用这个函数时,被传入的第一个参数所取代;
		//auto bf = bind(less<int>(), 40, placeholders::_1);
		//bf(19);

		cishu = count_if(myvector.begin(), myvector.end(), bind(
			less<int>(),  //临时对象
			40,
			placeholders::_1
			)
			);
		//a)bind:函数适配器中的绑定七;
		//b)less<int>():是个函数对象(仿函数),这里是个临时对象
		//c)count_if:是个算法;
		cout << cishu << endl;

		return;
	}
}
namespace _nmsp2
{
	//四:迭代器适配器
	//(4.1)reverse_iterator:二章九节讲解过:反向迭代器;

	//五:总结


	void func()
	{
		vector<int> iv = { 100, 200, 300 };
		for (vector<int>::reverse_iterator riter = iv.rbegin(); riter != iv.rend(); ++riter)
		{
			cout << *riter << endl;
		}

	}
}

int main()
{
	//_nmsp1::func();
	_nmsp2::func();
	return 1;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值