C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++STL <functional>

C++STL中<functional>的具体使用

作者:MzKyle

C++标准库中的<functional>库是一个强大的工具集,它提供了用于处理函数对象、函数绑定、函数包装等功能的设施,极大地增强了代码的灵活性和可复用性,感兴趣的可以了解一下

C++标准库中的 <functional> 库是一个强大的工具集,它提供了用于处理函数对象、函数绑定、函数包装等功能的设施,极大地增强了代码的灵活性和可复用性。

1. 函数对象(Functors)

函数对象,也被称作仿函数,是重载了 () 运算符的类的实例。其使用方式与普通函数调用类似,但函数对象可以携带状态,这使得它比普通函数更加灵活。

#include <iostream>

// 定义一个函数对象类
class Adder {
public:
    int operator()(int a, int b) const {
        return a + b;
    }
};

int main() {
    Adder adder;
    int result = adder(3, 4);
    std::cout << "3 + 4 = " << result << std::endl;
    return 0;
}

2.std::function

std::function 是一个通用的多态函数包装器,它可以存储、复制和调用任何可调用对象,包括普通函数、函数指针、成员函数指针、函数对象和 lambda 表达式等。

#include <iostream>
#include <functional>

// 普通函数
int add(int a, int b) {
    return a + b;
}

int main() {
    // 使用 std::function 包装普通函数
    std::function<int(int, int)> func = add;
    int result = func(3, 4);
    std::cout << "3 + 4 = " << result << std::endl;

    // 包装 lambda 表达式
    std::function<int(int, int)> lambdaFunc = [](int a, int b) { return a * b; };
    result = lambdaFunc(3, 4);
    std::cout << "3 * 4 = " << result << std::endl;
    return 0;
}

3.std::bind

std::bind 用于创建一个新的可调用对象,它将一个可调用对象和其部分或全部参数绑定在一起,允许进行部分参数绑定和参数重排。

#include <iostream>
#include <functional>

// 普通函数
int add(int a, int b) {
    return a + b;
}

int main() {
    // 使用 std::bind 绑定 add 函数的第一个参数为 3
    auto add3 = std::bind(add, 3, std::placeholders::_1);
    int result = add3(4);
    std::cout << "3 + 4 = " << result << std::endl;

    // 参数重排
    auto sub = [](int a, int b) { return a - b; };
    auto reversedSub = std::bind(sub, std::placeholders::_2, std::placeholders::_1);
    result = reversedSub(3, 4);
    std::cout << "4 - 3 = " << result << std::endl;
    return 0;
}

4.std::mem_fn

std::mem_fn 用于创建一个可调用对象,该对象可以调用类的成员函数。它可以处理成员函数指针,使得调用成员函数更加方便。

#include <iostream>
#include <functional>

class MyClass {
public:
    void print() const {
        std::cout << "Hello, World!" << std::endl;
    }
    int add(int a, int b) const {
        return a + b;
    }
};

int main() {
    MyClass obj;
    // 使用 std::mem_fn 包装成员函数
    auto printFunc = std::mem_fn(&MyClass::print);
    printFunc(obj);

    auto addFunc = std::mem_fn(&MyClass::add);
    int result = addFunc(obj, 3, 4);
    std::cout << "3 + 4 = " << result << std::endl;
    return 0;
}

5. 预定义的函数对象

<functional> 库提供了一系列预定义的函数对象,用于常见的操作,如算术运算、比较运算等。

算术运算

#include <iostream>
#include <functional>

int main() {
    std::plus<int> add;
    int result = add(3, 4);
    std::cout << "3 + 4 = " << result << std::endl;

    std::minus<int> sub;
    result = sub(4, 3);
    std::cout << "4 - 3 = " << result << std::endl;
    return 0;
}

比较运算

#include <iostream>
#include <functional>

int main() {
    std::greater<int> greater;
    bool isGreater = greater(4, 3);
    std::cout << "4 > 3: " << (isGreater ? "true" : "false") << std::endl;

    std::less<int> less;
    isGreater = less(4, 3);
    std::cout << "4 < 3: " << (isGreater ? "true" : "false") << std::endl;
    return 0;
}

6.std::ref和std::cref

std::refstd::cref 用于创建引用包装器,当需要将引用传递给 std::bind 或其他需要按值传递参数的地方时非常有用。

#include <iostream>
#include <functional>

void increment(int& num) {
    ++num;
}

int main() {
    int num = 3;
    auto func = std::bind(increment, std::ref(num));
    func();
    std::cout << "num = " << num << std::endl;
    return 0;
}

7.std::hash

std::hash 是一个模板类,用于生成对象的哈希值,常用于实现哈希表等数据结构。

#include <iostream>
#include <functional>

int main() {
    std::hash<int> hasher;
    int num = 3;
    std::size_t hashValue = hasher(num);
    std::cout << "Hash value of " << num << " is " << hashValue << std::endl;
    return 0;
}

8.std::not_fn(C++17 起)

std::not_fn 用于创建一个新的可调用对象,该对象返回原可调用对象结果的逻辑非。

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>

bool isEven(int num) {
    return num % 2 == 0;
}

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    auto isOdd = std::not_fn(isEven);
    auto oddCount = std::count_if(numbers.begin(), numbers.end(), isOdd);
    std::cout << "Number of odd numbers: " << oddCount << std::endl;
    return 0;
}

总结

<functional> 库提供了丰富的工具来处理函数对象和函数调用,通过这些工具可以提高代码的灵活性和可维护性。合理使用这些工具可以让代码更加简洁和高效,特别是在实现回调机制、函数绑定、成员函数调用等场景中发挥重要作用。

到此这篇关于C++STL中<functional>的具体使用的文章就介绍到这了,更多相关C++STL <functional>内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文