【问题标题】:How to convert scala function as parameter in c++?如何将scala函数转换为c ++中的参数?
【发布时间】:2017-04-18 07:25:45
【问题描述】:

所以我想在 c++ 中实现函数式编程,就像在 scala 中一样。

假设我在 scala 中有这个代码 sn-p:

def exampleA(l: List[P], f: P => Option[P]): Option[P] = l match {
  case Nil => None
  case x :: l => val a = f(x); if ( a.notDefined ) exampleA(l, f) else a
}

它接受一个 P 列表和 a function as parameter。它将返回列表中的第一个元素。

现在我想在 C++ 中使用相同的方法,我已经很困惑了,因为没有关于调用函数作为参数的信息。

这就是我目前所拥有的:

typedef vector<pair<int,int> > P;

P exampleB( P (*f)(int,Path) ) {
  cout << (*f)->first << endl;
}


P exampleBA(int dim, Path path) {
  exampleB(exampleBA);
}

我想在 example() 方法中访问 P 中的 pair 的 first 元素。

我该怎么做?

【问题讨论】:

  • 我不建议在 c++ 中使用与 scala 中相同的样式。它们是不同的野兽,如果你的代码被 c++ 程序员使用,他们会有点困惑。你知道...在罗马时,做罗马人会做的事。

标签: c++ scala function functional-programming function-parameter


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
相关资源
最近更新 更多