bind1st bind2snd解析

本文详细介绍了C++ STL中的bind1st和bind2nd适配器的功能与使用方法,并通过示例代码展示了如何将二元运算符转化为一元运算符,以便在算法中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件:#include <algorithm>

先不要被吓到,其实这两个配接器很简单。
首先,他们都在头文件<functional>中定义。
其次,bind就是绑定的意思,而1st就代表first,2nd就代表second,现在名在可以很快记住了。
再次,他们的申明是一样的,都是(const Operation& op, const T& x)

简单的说,bind1st(const Operation& op, const T& x)就是这么一个操作:x operation value,而bind2nd(const Operation& op, const T& x)就是这么一个操作:value operation x,其中value是被应用bind的对象。这两个配接器都用于将一个二元算子转换成一个一元算子。下面来看一段代码吧!

//Coded by www.programlife.net
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	vector<int> coll;
	for(int i = 1; i <= 10; ++i)
	{
		coll.push_back(i);
	}
	//查找元素值大于10的元素的个数
	//也就是使得10 < elem成立的元素个数 
	int res = count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));
	cout << res << endl;
	//查找元素值小于10的元素的个数
	//也就是使得elem < 10成立的元素个数 
	res = count_if(coll.begin(), coll.end(), bind2nd(less<int>(), 10));
	cout << res << endl;
	
	return 0;
}

程序的输出结果是0 9

来自:http://www.programlife.net/stl-bind1st-bind2nd-demo.html



如果某个函数或函数对象调用需要两个参数,bind1st会邦定我们在调用bind1st的函数调用表达式中给出的参数为函数对象的第一个参数,而形成一个新的函数对象,也就是将一个二元(两个参数)函数对象的第一个参数固定下来,而形成一个新的只需要一个参数的一元函数对象,而将这个新的函数对象应用到算法时,相当于只有一个参数了(第一个参数已经被绑定),这时候情况就跟上面的例子完全相同了。例如:
void print(int standard,int n) // 创建一个需要两个参数的函数
{
if(n>=standard)
cout<
}
int main()
{
vector v = {63,47,93,75};
// 打印容器中大于60的数据
for_each(v.begin(),v.end(),
bind1st(ptr_fun(print),60)); // 这里,ptr_fun的作用是将一个函数指针构造成一个函数对象(你也可以直接使用函数对象),而bind1st就是帮定这个函数对象的第一个参数为60,也就是standard为60,整个函数变为print(60, int n),只有一个参数了,这是完全跟上面的例子相同了。
return 0;
}
bind2nd的原理是一样的,只不过是为了邦定函数的第二个参数。
而在新标准C++11中,还有bind函数,可以帮定更多参数。这些函数的使用是有点复杂,多加练习,熟悉后就好了。

来自:http://imcc.blogbus.com/logs/162062257.html


dmesg | grep -e i915 -e snd_hda -e drm -e amd [ 6.559265] systemd[1]: Starting Load Kernel Module drm... [ 6.704955] systemd[1]: modprobe@drm.service: Deactivated successfully. [ 6.705017] systemd[1]: Finished Load Kernel Module drm. [ 7.965413] i915 0000:00:02.0: enabling device (0000 -> 0003) [ 7.968541] i915 0000:00:02.0: Direct firmware load for i915/kbl_dmc_ver1_04.bin failed with error -2 [ 7.968544] i915 0000:00:02.0: [drm] Failed to load DMC firmware i915/kbl_dmc_ver1_04.bin. Disabling runtime power management. [ 7.968545] i915 0000:00:02.0: [drm] DMC firmware homepage: https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmwar5 [ 8.045478] [drm] radeon kernel modesetting enabled. [ 8.045630] [drm] initializing kernel modesetting (OLAND 0x1002:0x6611 0x1642:0x1869 0x87). [ 8.045741] [drm] Changing default dispclk from 480Mhz to 600Mhz [ 8.045751] [drm] Detected VRAM RAM=2048M, BAR=256M [ 8.045752] [drm] RAM width 64bits DDR [ 8.045756] [drm] radeon: 2048M of VRAM memory ready [ 8.045757] [drm] radeon: 2048M of GTT memory ready. [ 8.045761] [drm] Loading oland Microcode [ 8.045772] [drm:si_init.cold [radeon]] *ERROR* Failed to load firmware! [ 8.045810] [drm] radeon: finishing device. [ 8.053014] [drm] radeon: ttm finalized [ 8.253678] snd_hda_intel 0000:00:1f.3: enabling device (0000 -> 0002) [ 8.253801] snd_hda_intel 0000:01:00.1: enabling device (0000 -> 0002) [ 8.253840] snd_hda_intel 0000:01:00.1: Force to non-snoop mode [ 8.460880] i915 0000:00:02.0: [drm] failed to retrieve link info, disabling eDP [ 8.461092] i915 0000:00:02.0: [drm] [ENCODER:94:DDI B/PHY B] is disabled/in DSI mode with an ungated DDI clock, gate it [ 8.461094] i915 0000:00:02.0: [drm] [ENCODER:105:DDI C/PHY C] is disabled/in DSI mode with an ungated DDI clock, gate it [ 8.461095] i915 0000:00:02.0: [drm] [ENCODER:111:DDI D/PHY D] is disabled/in DSI mode with an ungated DDI clock, gate it [ 8.493388] [drm] Initialized i915 1.6.0 20201103 for 0000:00:02.0 on minor 0 [ 8.507890] snd_hda_intel 0000:00:1f.3: bound 0000:00:02.0 (ops i915_audio_component_bind_ops [i915]) [ 8.527147] [drm] amdgpu kernel modesetting enabled. [ 8.527241] amdgpu: CRAT table disabled by module option [ 8.527242] amdgpu: Virtual CRAT table created for CPU [ 8.527267] amdgpu: Topology: Add CPU node [ 8.527307] amdgpu 0000:01:00.0: amdgpu: SI support provided by radeon. [ 8.527308] amdgpu 0000:01:00.0: amdgpu: Use radeon.si_support=0 amdgpu.si_support=1 to override. [ 10.950939] i915 0000:00:02.0: [drm] Cannot find any crtc or sizes [ 13.714849] i915 0000:00:02.0: [drm] Cannot find any crtc or sizes [ 16.506338] i915 0000:00:02.0: [drm] Cannot find any crtc or sizes [ 16.560082] snd_hda_codec_realtek hdaudioC0D0: autoconfig for ALC662 rev3: line_outs=1 (0x14/0x0/0x0/0x0/0x0) type:line [ 16.560092] snd_hda_codec_realtek hdaudioC0D0: speaker_outs=0 (0x0/0x0/0x0/0x0/0x0) [ 16.560096] snd_hda_codec_realtek hdaudioC0D0: hp_outs=1 (0x1b/0x0/0x0/0x0/0x0) [ 16.560100] snd_hda_codec_realtek hdaudioC0D0: mono: mono_out=0x0 [ 16.560102] snd_hda_codec_realtek hdaudioC0D0: inputs: [ 16.560105] snd_hda_codec_realtek hdaudioC0D0: Front Mic=0x19 [ 16.560108] snd_hda_codec_realtek hdaudioC0D0: Rear Mic=0x18 [ 16.560110] snd_hda_codec_realtek hdaudioC0D0: Line=0x1a
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值