怎样做网站营销,营销网络信息化的作用有哪些,四川省建设厅信息平台,上海企业网站建设哪家好C 标准库在 functional 头文件中为我们提供了一套非常方便的预定义函数对象#xff08;也称为“仿函数”或 “functor”#xff09;#xff0c;它们可以像变量一样直接传递给 std::reduce 和其他标准算法。
你提到的 std::bit_or 和 std::multiplies 就是其中的成员…C 标准库在 functional 头文件中为我们提供了一套非常方便的预定义函数对象也称为“仿函数”或 “functor”它们可以像变量一样直接传递给 std::reduce 和其他标准算法。
你提到的 std::bit_or 和 std::multiplies 就是其中的成员。这些函数对象的好处是代码更具可读性并且可以避免手写简单的 lambda 表达式。
从 C14 开始这些函数对象大多有了“透明”版本使用 std::plus 而不是 std::plusint这让它们使用起来更加方便因为你不需要手动指定类型编译器会自动推断。
下面是这些常用函数对象的分类介绍和 std::reduce 的使用示例。1. 算术操作 (Arithmetic Functors)
这些是最常见的聚合操作。函数对象 (透明版本)作用C 操作符示例 reduce 初始值std::plus()加法a b0 或 0.0std::minus()减法a - bN/A (不满足结合律)std::multiplies()乘法a * b1 或 1.0std::divides()除法a / bN/A (不满足结合律)std::modulus()取模a % bN/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::vectorint 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()按位或abstd::bit_xor()按位异或a ^ b0std::bit_not()按位取反 (一元)~a(不适用于reduce)代码示例:
#include iostream
#include vector
#include numeric
#include functionalint main() {std::vectorunsigned 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::vectorunsigned 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 btruestd::logical_or()逻辑或astd::logical_not()逻辑非 (一元)!a(不适用于reduce)代码示例:
#include iostream
#include vector
#include numeric
#include functionalint main() {std::vectorbool 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 bstd::not_equal_to()不等于a ! bstd::greater()大于a bstd::less()小于a bstd::greater_equal()大于等于a bstd::less_equal()小于等于a b求最值的最佳实践是使用 Lambda 表达式因为它们更清晰。
代码示例:
#include iostream
#include vector
#include numeric
#include algorithm // for std::min/maxint main() {std::vectorint 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 时选择正确的初始值至关重要它决定了整个计算的基础。