C++ 标准库在 <functional>
头文件中为我们提供了一套非常方便的预定义函数对象(也称为“仿函数”或 “functor”),它们可以像变量一样直接传递给 std::reduce
和其他标准算法。
你提到的 std::bit_or
和 std::multiplies
就是其中的成员。这些函数对象的好处是代码更具可读性,并且可以避免手写简单的 lambda 表达式。
从 C++14 开始,这些函数对象大多有了“透明”版本(使用 std::plus<>
而不是 std::plus<int>
),这让它们使用起来更加方便,因为你不需要手动指定类型,编译器会自动推断。
下面是这些常用函数对象的分类介绍和 std::reduce
的使用示例。
1. 算术操作 (Arithmetic Functors)
这些是最常见的聚合操作。
函数对象 (透明版本) | 作用 | C++ 操作符 | 示例 reduce 初始值 |
---|---|---|---|
std::plus<>() | 加法 | a + b | 0 或 0.0 |
std::minus<>() | 减法 | a - b | N/A (不满足结合律) |
std::multiplies<>() | 乘法 | a * b | 1 或 1.0 |
std::divides<>() | 除法 | a / b | N/A (不满足结合律) |
std::modulus<>() | 取模 | a % b | N/A (不满足结合律) |
std::negate<>() | 取反 (一元) | -a | (不适用于reduce ) |
注意: minus
和 divides
通常不用于 std::reduce
,因为它们不满足并行计算所必需的结合律。(((a-b)-c)-d)
的结果与 (a-b) + (c-d)
通常是不同的。
代码示例:
#include <iostream>
#include <vector>
#include <numeric>
#include <functional> // 必须包含int main() {std::vector<int> nums = {1, 2, 3, 4, 5};// 求和int sum = std::reduce(nums.begin(), nums.end(), 0, std::plus<>());std::cout << "Sum: " << sum << std::endl; // 输出: 15// 求积long long product = std::reduce(nums.begin(), nums.end(), 1LL, std::multiplies<>());std::cout << "Product: " << product << std::endl; // 输出: 120
}
2. 位运算 (Bitwise Functors)
这些在你需要对一系列整数进行位操作时非常有用。
函数对象 (透明版本) | 作用 | C++ 操作符 | 示例 reduce 初始值 |
---|---|---|---|
std::bit_and<>() | 按位与 | a & b | ~0 (所有位都为1) |
std::bit_or<>() | 按位或 | `a | b` |
std::bit_xor<>() | 按位异或 | a ^ b | 0 |
std::bit_not<>() | 按位取反 (一元) | ~a | (不适用于reduce ) |
代码示例:
#include <iostream>
#include <vector>
#include <numeric>
#include <functional>int main() {std::vector<unsigned int> flags = {0b0001, 0b0010, 0b1000}; // 1, 2, 8// 将所有标志位合并 (OR)// 0 | 1 | 2 | 8 = 11 (0b1011)unsigned int all_flags = std::reduce(flags.begin(), flags.end(), 0u, std::bit_or<>());std::cout << "All flags (OR): " << all_flags << std::endl; // 输出: 11// 找到所有共有位 (AND)std::vector<unsigned int> masks = {0b1101, 0b0111, 0b1111};// 0b1101 & 0b0111 & 0b1111 = 0b0101 (5)// 初始值需要是全1,否则任何数与0做&运算都会得到0unsigned int common_bits = std::reduce(masks.begin(), masks.end(), ~0u, std::bit_and<>());std::cout << "Common bits (AND): " << common_bits << std::endl; // 输出: 5
}
3. 逻辑运算 (Logical Functors)
这些通常用于聚合布尔值。
函数对象 (透明版本) | 作用 | C++ 操作符 | 示例 reduce 初始值 |
---|---|---|---|
std::logical_and<>() | 逻辑与 | a && b | true |
std::logical_or<>() | 逻辑或 | `a | |
std::logical_not<>() | 逻辑非 (一元) | !a | (不适用于reduce ) |
代码示例:
#include <iostream>
#include <vector>
#include <numeric>
#include <functional>int main() {std::vector<bool> conditions = {true, false, true};// 检查是否所有条件都为真 (AND)bool all_true = std::reduce(conditions.begin(), conditions.end(), true, std::logical_and<>());std::cout << "All true? " << std::boolalpha << all_true << std::endl; // 输出: false// 检查是否至少一个条件为真 (OR)bool any_true = std::reduce(conditions.begin(), conditions.end(), false, std::logical_or<>());std::cout << "Any true? " << std::boolalpha << any_true << std::endl; // 输出: true
}
4. 比较运算 (Comparison Functors) - 用于求最值
比较运算符本身不直接用于聚合,但它们是构建求最大/最小值逻辑的核心。虽然你可以直接使用 lambda 表达式 std::min
或 std::max
,但了解它们的存在也很有用。
函数对象 (透明版本) | 作用 | C++ 操作符 |
---|---|---|
std::equal_to<>() | 等于 | a == b |
std::not_equal_to<>() | 不等于 | a != b |
std::greater<>() | 大于 | a > b |
std::less<>() | 小于 | a < b |
std::greater_equal<>() | 大于等于 | a >= b |
std::less_equal<>() | 小于等于 | a <= b |
求最值的最佳实践是使用 Lambda 表达式,因为它们更清晰。
代码示例:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm> // for std::min/maxint main() {std::vector<int> nums = {10, -5, 100, 30, -20};if (nums.empty()) return 1;// 求最大值// 初始值设为第一个元素,避免空容器或所有元素都为负数的问题int max_val = std::reduce(std::next(nums.begin()), // 从第二个元素开始nums.end(), // 到末尾nums.front(), // 初始值为第一个元素[](int a, int b) { return std::max(a, b); } // 使用 std::max);std::cout << "Max value: " << max_val << std::endl; // 输出: 100// 求最小值int min_val = std::reduce(std::next(nums.begin()),nums.end(),nums.front(),[](int a, int b) { return std::min(a, b); } // 使用 std::min);std::cout << "Min value: " << min_val << std::endl; // 输出: -20
}
总结
<functional>
头文件是你的好朋友,它提供了丰富的预定义函数对象。- 使用透明函数对象(如
std::plus<>()
)是现代C++的最佳实践,代码更简洁。 - 对于简单的、标准的操作(加、乘、位运算),直接使用这些函数对象非常方便。
- 对于更复杂的逻辑,尤其是求最值,Lambda 表达式通常是更灵活、更具可读性的选择。
- 使用
reduce
时,选择正确的初始值至关重要,它决定了整个计算的基础。