Binary &Op是什么

打印 上一主题 下一主题

主题 977|帖子 977|积分 2931

前言

在并行开发时我们经常会用到Pstream::gather()函数或是全局函数reduce()或者其他,需要输入参数Binary &Op,本篇主要讨论Binary &Op是什么
  1. template<class T, class BinaryOp>
  2. void reduce
  3. (
  4.     T& Value,
  5.     const BinaryOp& bop, // 这里要输入什么参数
  6.     const int tag,
  7.     const label comm,
  8.     label& request
  9. )
  10. {
  11.     NotImplemented;
  12. }
复制代码
Binary &Op

单从名字上看,猜是一个二进制的操作,类似一组操作返回一个二进制的标记
然后去openfoam官网去找,找不到Binary &Op的任何释义
去网上找,发现了一点端倪
c++标准库中有应用Binary &Op这个输入参数,使用的函数也叫reduce,详情可见该网页
这里顺便说几个学习C++不错的网址,查询起来很方便
  1. https://en.cppreference.com/w/
  2. https://learncpp-cn.github.io/
  3. https://cplusplus.com/
  4. https://www.tutorialspoint.com/index.htm
  5. https://awesome-cpp.readthedocs.io/en/latest/README.html#
  6. https://stackoverflow.org.cn/
复制代码
标准库对输入参数binary_op的解释是:
将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject) 。
我们再看下类似的std::transform,详情可见该网页
可能实现的版本有
  1. template<class InputIt1, class InputIt2,
  2.          class OutputIt, class BinaryOperation>
  3. OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
  4.                    OutputIt d_first, BinaryOperation binary_op)
  5. {
  6.     while (first1 != last1) {
  7.         *d_first++ = binary_op(*first1++, *first2++);
  8.     }
  9.     return d_first;
  10. }
复制代码
在这个程序中我们看到输入的参数可以传的参数可以函数指针,也可以是仿函数,而且这个仿函数需要传两个参数,按要求送达至返回值中
但按照C++的风格更喜欢是仿函数了
所以我们经常能看到类似这样的函数使用模式
[code]#include #include #include int main () {   bool foo[] = {true,true,false,false};   bool bar[] = {true,false,true,false};   bool result[4];   std::transform (foo, foo+4, bar, result, std::logical_or());   std::cout
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表