c++ 新特性 std::bind 简单实验

本文详细介绍了C++11中的std::bind函数,包括如何使用std::placeholders::_1、std::placeholders::_2等占位符来绑定函数参数,以及在不同场景下的实验和应用实例。

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

1.概要

std::bind 是 C++11 引入的一个功能,它用于绑定函数/可调用对象到特定的参数,并生成一个新的可调用对象。这个新的可调用对象可以稍后调用,就像调用原始函数/可调用对象一样,但会带有预先绑定的参数。

std::placeholders::_1、std::placeholders::_2 等是 C++11 引入的占位符,用于 std::bind 表达式中,以指示哪些参数将在稍后调用绑定后的函数对象时被提供。这些占位符用于替换被绑定的函数或可调用对象的某些参数。
在 std::bind 的上下文中,std::placeholders::_1 表示第一个(从 1 开始计数)没有被显式绑定的参数,std::placeholders::_2 表示第二个,以此类推。当绑定后的函数对象被调用时,你需要为这些占位符提供具体的值。

2.代码分析

2.1std::bind 简单实验
2.1.1 代码
#include <iostream>  
#include <functional> // 包含 std::bind 的头文件  
using namespace std;

namespace t1 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b) {
        return a + b;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 你也可以绑定多个参数  
        auto bound_add_ten_fifteen = std::bind(add, 10, 15);
        int another_result = bound_add_ten_fifteen(); // 这相当于调用 add(10, 15)  
        std::cout << "Another Result: " << another_result << std::endl; // 输出: Another Result: 25  
    }
}
2.1.2 实验结果
Another Result: 25 == 10(占位)+15(占位)

在这个例子中,我们首先定义了一个简单的 add 函数,它接受两个整数参数并返回它们的和。然后,我们使用 std::bind 来绑定 aadd 函数的两个参数,生成一个不接受任何参数的可调用对象 bound_add_ten_fifteen。这个可调用对象直接返回 add(10, 15) 的结果。

注意,在绑定参数时,我们使用 std::placeholders::_1、std::placeholders::_2 等来表示原始函数/可调用对象的参数。这些占位符在 std::bind 的参数列表中按照它们在原始函数/可调用对象中的顺序来替代实际的参数。

2.2 std::placeholders::_1 等占位符实验
2.2.1 代码
/// <summary>
/// std::placeholders::_1 是 C++11 引入的占位符,用于 std::bind 表达式中,
/// 以指示哪些参数将在稍后调用绑定后的函数对象时被提供。
/// 这些占位符用于替换被绑定的函数或可调用对象的某些参数。
/// </summary>
namespace t2 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b) {
        return a + b;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  

    }
}
2.2.3 实验结果
Result: 8 = 5(占位)+3

2.3 尝试多个占位符
2.3.1 代码
namespace t3 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b,int c) {
        return a + b+c;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5 
        // std::placeholders::_0 表示一个占位符 类似于数据的[0] 
        // std::placeholders::_1 表示一个占位符 类似于数据的[1]
        // std::placeholders::_2 表示一个占位符 类似于数据的[2]
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, std::placeholders::_2);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3,4); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}
2.3.2 运行结果
Result: 12  = 5(占位)+3+4(占位)

2.4 尝试交叉绑定参数
//尝试交叉绑定参数
// 参数,占位符,参数
namespace t4 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b, int c) {
        return a + b + c;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, 7);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}

2.5 我在已经有值的位置,输入了,是使用预定义的参数,还是我目前的
2.5.1 代码
//我在已经有值的位置,输入了,是使用预定义的参数,还是我目前的
//结果:使用预定义的数据,即使std::bind生成的函数不报错,也没有用
namespace t5 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b, int c) {
        return a + b + c;
    }
    void test() {
        
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, 7);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        // 这是即使输入了9也不会有用,因为只有一个占位符需要天才
        int result = bound_add_five(3,9); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}
2.5.2 运行结果
Result: 15 = 5(占位)+3+7(占位)

3.代码

#include <iostream>  
#include <functional> // 包含 std::bind 的头文件  
using namespace std;

namespace t1 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b) {
        return a + b;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 你也可以绑定多个参数  
        auto bound_add_ten_fifteen = std::bind(add, 10, 15);
        int another_result = bound_add_ten_fifteen(); // 这相当于调用 add(10, 15)  
        std::cout << "Another Result: " << another_result << std::endl; // 输出: Another Result: 25  
    }
}

/// <summary>
/// std::placeholders::_1 是 C++11 引入的占位符,用于 std::bind 表达式中,
/// 以指示哪些参数将在稍后调用绑定后的函数对象时被提供。
/// 这些占位符用于替换被绑定的函数或可调用对象的某些参数。
/// </summary>
namespace t2 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b) {
        return a + b;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  

    }
}

//尝试多个占位符
namespace t3 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b,int c) {
        return a + b+c;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5 
        // std::placeholders::_0 表示一个占位符 类似于数据的[0] 
        // std::placeholders::_1 表示一个占位符 类似于数据的[1]
        // std::placeholders::_2 表示一个占位符 类似于数据的[2]
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, std::placeholders::_2);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3,4); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}

//尝试交叉绑定参数
// 参数,占位符,参数
namespace t4 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b, int c) {
        return a + b + c;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, 7);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        int result = bound_add_five(3); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}

//我在已经有值的位置,输入了,是使用预定义的参数,还是我目前的
//结果:使用预定义的数据,即使std::bind生成的函数不报错,也没有用
namespace t5 {
    // 一个简单的函数,接受两个整数参数并返回它们的和  
    int add(int a, int b, int c) {
        return a + b + c;
    }
    void test() {
        cout << "----------------------------------------------\n";
        // 使用 std::bind 绑定 add 函数的第一个参数为 5  
        // std::placeholders::_1 表示一个占位符
        auto bound_add_five = std::bind(add, 5, std::placeholders::_1, 7);

        // 现在 bound_add_five 是一个新的可调用对象,它接受一个整数参数  
        // 并返回该整数与 5 的和  
        // 这是即使输入了9也不会有用,因为只有一个占位符需要天才
        int result = bound_add_five(3,9); // 这相当于调用 add(5, 3)  
        std::cout << "Result: " << result << std::endl; // 输出: Result: 8  
    }
}

int main() {
    
    t1::test();
    t2::test();
    t3::test();
    t4::test();
    t5::test();
    return 0;
}

4.运行结果

----------------------------------------------
Another Result: 25
----------------------------------------------
Result: 8
----------------------------------------------
Result: 12
----------------------------------------------
Result: 15
----------------------------------------------
Result: 15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值