【手撕代码】手动实现一个bind1st和bind2nd绑定器

news/2024/7/10 1:47:51 标签: bind, js, c++, vue, lambda
// 08 C++绑定器和函数对象.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <vector> 
#include <functional>
#include <algorithm>
#include <ctime>
using namespace std;

#if 0
/*
绑定器和函数对象operator()

1.C++ STL中的绑定器  
bind1st : operator()的第一个形参变量绑定成一个确定的值
bind2nd : operator()的第二个形参变量绑定成一个确定的值

2.C++11从Boost库中引入了bind绑定器和function函数对象机制

3.lambda表达式 底层依赖函数对象的机制实现的
*/
template<typename Container>
void showContainer(Container &con)
{
	typename Container::iterator it = con.begin();
	for (; it != con.end(); ++it)
	{
		cout << *it << " ";
	}
	cout << endl;
}

template<typename Iterator, typename Compare>
Iterator my_find_if(Iterator first, Iterator last, Compare comp)
{
	for (; first != last; ++first)
	{
		if (comp(*first)) // comp.operator()(*first)
		{
			return first;
		}
	}
	return last;
}


template<typename Compare, typename T>
class _mybind1st // 绑定器是函数对象的一个应用
{
public:
	_mybind1st(Compare comp, T val)
		:_comp(comp), _val(val) 
	{}
	bool operator()(const T &second)
	{
		return _comp(_val, second); // greater
	}
private:
	Compare _comp;
	T _val;
};

//mybind1st(greater<int>(), 70)
template<typename Compare, typename T>
_mybind1st<Compare,T> mybind1st(Compare comp, const T &val)
{
	// 直接使用函数模板,好处是,可以进行类型的推演
	return _mybind1st<Compare, T>(comp, val);
}

int main()
{
	vector<int> vec;
	srand(time(nullptr));
	for (int i = 0; i < 20 ; ++i)
	{
		vec.push_back(rand() % 100 + 1);
	}

	showContainer(vec);
	sort(vec.begin(), vec.end()); // 默认小到大排序
	showContainer(vec);

	// greater 二元函数对象
	sort(vec.begin(), vec.end(), greater<int>()); // 大到小排序
	showContainer(vec);

	/* 
	把70按顺序插入到vec容器当中   找第一个小于70的数字
	operator()(const T &val)
	greater   a > b
	less      a < b
	绑定器 + 二元函数对象 =》 一元函数对象
	bind1st: + greater bool operator()(70, const _Ty& _Right)
	bind2nd: + less bool operator()(const _Ty& _Left, 70)
	*/
	auto it1 = my_find_if(vec.begin(), vec.end(),
		mybind1st(greater<int>(), 70));
	//auto it1 = my_find_if(vec.begin(), vec.end(),
		//bind2nd(less<int>(), 70));
	if (it1 != vec.end())
	{
		vec.insert(it1, 70);
	}
	showContainer(vec);

	return 0;
}
#endif

http://www.niftyadmin.cn/n/923556.html

相关文章

function函数对象应用示例

#include "pch.h" #include <iostream> #include <vector> #include <map> #include <functional> // 使用function函数对象类型 #include <algorithm> #include <ctime> #include <string> using namespace std;#if 0 …

lambda表达式实例

#include "pch.h" #include <iostream> #include <algorithm> #include <vector> #include <functional> #include <map> #include <memory> #include <queue> using namespace std;/* 既然lambda表达式只能使用在语句当中…

生产者消费者线程间通信模型

#include "pch.h" #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> // C STL所有的容器都不是线程安全 using namespace std; #if 0 /* unique_lock condition_variable 1.…

C++模拟车站窗口卖票

#include "pch.h" #include <iostream> #include <thread> #include <mutex> #include <list> using namespace std;#if 0 /* C thread 模拟车站三个窗口卖票的程序线程间的互斥 》 互斥锁mutex 》 lock_guard封装mutex */ int ticketCount …

线程安全的懒汉/饿汉单例模式的实现

#include "pch.h" #include <iostream> #include <mutex> using namespace std;/* 单例模式&#xff1a;一个类不管创建多少次对象&#xff0c;永远只能得到该类型一个对象的实例 A *p1 new A(); A *p2 new A(); A *p3 new A(); 常用到的&#xff0c;…

深度遍历迷宫搜索C++实现

// 深度遍历迷宫路径搜索.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 //#include "pch.h" #include <iostream> #include <stack> using namespace std;#if 0 /* 深度遍历搜索迷宫路径&#xff0c;软件运行要求如下&#xf…

数据结构中二叉树的度

首先说说什么是度&#xff1a;通俗的讲二叉树中连接节点和节点的线就是度&#xff0c;有n个节点&#xff0c;就有n-1个度&#xff0c;节点数总是比度要多一个&#xff0c;那么度为0的节点一定是叶子节点&#xff0c;因为该节点的下面不再有线&#xff1b;度为1的节点即&#xf…

大数的加减法

#include "pch.h" #include <iostream> #include <string> #include <algorithm> using namespace std;#if 0 // 编程题目&#xff1a;请实现以下类的方法&#xff0c;完成大数的加减法 class BigInt { public:BigInt(string str) :strDigit(str) …