C++进阶STL-函数对象设配器

函数对象设备器

  • 函数对象设备器是完成一些配接工作,配接包括 绑定(bind),否定(negate),以及对一般函数或成员函数的修饰,使其成为函数对象


绑定设备器

  • 作用:将二元的函数对象转成一元的函数对象



bind1st、bind2nd

  • bind1st:将参数绑定为函数对象的第一个参数
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;


struct Print : public binary_function<int,int,void>
{
	void operator()(int val1,int val2)const
	{
		cout << "val1:" << val1<<" val2:"<< val2<< "------->val1+val2:" << val1+ val2 << endl;
	}
};

int main()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	for_each(v1.begin(), v1.end(), bind1st(Print(),100));

   return 0;
}

在这里插入图片描述

  • bind2nd:将参数绑定为函数对象的第二个参数
for_each(v1.begin(), v1.end(), bind2nd(Print(),100));

在这里插入图片描述




not1、not2


- not2:对二元函数对象取反
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;


struct Print
{
	void operator()(int val1)
	{
		cout  <<" " <<val1;
	}
};

struct Compare:public binary_function<int,int,bool>
{
	bool operator()(int v1, int v2)const
	{
		return v1 > v2;   
	}
};

int main()
{
	
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
    }

	for_each(v1.begin(), v1.end(), Print()); cout << endl;   //原数据

	sort(v1.begin(), v1.end(), Compare());        //从大到小排
	for_each(v1.begin(), v1.end(), Print()); cout << endl; 

	sort(v1.begin(), v1.end(), not2(Compare()));  //经过not2适配器,从小到大排序
	for_each(v1.begin(), v1.end(), Print()); cout << endl; 

   return 0;
}

结果:  第一行:原始数据
    第二行:从大到小排序
    第三行:经过not2设备器 -> 从小到大排序
在这里插入图片描述

  • not1:对一元函数对象取反
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;


struct Print
{
	void operator()(int val1)
	{
		cout  <<" " <<val1;
	}
};

struct Greater5:public unary_function<int,bool>
{
	bool operator()(int v1)const
	{
		return v1 > 5;
	}
};

int main()
{
	
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
    }

	for_each(v1.begin(), v1.end(), Print()); cout << endl;

	vector<int>::iterator it1=find_if(v1.begin(), v1.end(), Greater5());  //找到第一个 >5 的值
	cout << *(it1) << endl;

	vector<int>::iterator it2 = find_if(v1.begin(), v1.end(), not1(Greater5())); //找到第一个小于5的值
	cout << *(it2) << endl;

   return 0;
}

结果:第一个 >5 的值 :6     经过not1设配器    第一个小于5的值:0
在这里插入图片描述




ptr_fun

  • ptr_fun:将普通函数修饰成函数对象,普通函数没法进行适配器操作
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
void Print(int v1, int v2)
{
	cout << v1 << " " << v2 << endl;
}


int main()
{
	
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
        }

	for_each(v1.begin(), v1.end(), bind2nd(ptr_fun(Print),10)); 
	
   return 0;
}

在这里插入图片描述



mem_fun_ref

  • mem_fun_ref:修饰成员函数,如果存到的是 对象使用men_fun_ref
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{
	}
	void Print()
	{
		cout << " age: " << m_age <<"     id:" << m_id << endl;
	}

	int m_age;
	int m_id;
};

int main()
{
	vector <Person> v;
	Person p1(10, 20) ;
	Person p2(20, 30) ;
	Person p3(30, 40) ;

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);


	for_each(v.begin(), v.end(), mem_fun_ref(&Person::Print));  //使用自己的成员函数 进行打印

   return 0;
}

结果:
在这里插入图片描述



mem_fun

  • mem_fun:修饰成员函数,如果存到的是 对象指针使用men_fun
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{
	}
	void Print()
	{
		cout << " age: " << m_age <<"     id:" << m_id << endl;
	}

	int m_age;
	int m_id;
};

int main()
{
	vector <Person*> v;   //存放的是对象指针
	Person p1(10, 20) ;
	Person p2(20, 30) ;
	Person p3(30, 40) ;

	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);


	for_each(v.begin(), v.end(), mem_fun(&Person::Print));

   return 0;
}


结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zzyczzyc/article/details/82953013