i beginner in c++ , want simple example of composite function. example, in matlab, can write
a = @(x) 2*x b = @(y) 3*y a(b(1))
answer 6
i searched following questions. function composition in c++ / c++11 , function composition in c++
but created using advanced features, such templates, not familiar @ time. there simple , more direct way achieve this? in above matlab code, user not need know implementation of function handles. user can use proper syntax result. there such way in c++?
** edit:**
in above code, putting value @ end. however, if want pass result third function, matlab can still consider function. but, how in c++?
for example, in addition above code, consider code:
c = @(p,q) a(p)* b(q) %this results function c(1,2) answer=12 d = @(r) a(b(r)) d(1) answer=6 function [ output1 ] = f1( arg1 ) val = 2.0; output1 = feval(arg1,val) end f1(d) answer = 12
in code, c
takes 2 functions input , d
composite function. in next example, function f1
takes function argument , use matlab builtin function feval
evaluate function @ val
.
how can achieve in c++?
how about:
#include <iostream> int main(int, char**) { auto = [](int x) { return 2 * x; }; auto b = [](int y) { return 3 * y; }; (int = 0; < 5; ++i) std::cout << << " -> " << a(b(i)) << std::endl; return 0; }
Comments
Post a Comment