由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个c++ 里functor的问题
相关主题
sizeof()的问题c++ typedef 一问
一个C++ template的问题how to initialize this struct.
请教一个C++ typedef的问题为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?
用STL map的时候怎么自己定义大小比较的关系请教函数 INIT 怎么能free memory
[合集] 大家看看我这个C++ STL Functor那里写错了difference between FILE and struct FILE
请教C++中的unordered_set请问可以这样定义struct吗?
haskell 真是逆天, 各种特殊符号都有特殊用途[合集] Interview Question
haskell 真是逆天, 各种特殊符号都有特殊用途ask a question about struct in C programming
相关话题的讨论汇总
话题: vecdoub话题: doub话题: double话题: int话题: funcd
进入Programming版参与讨论
1 (共1页)
i*****n
发帖数: 36
1
我想在c++ 里做optimization, 在网上找到如下的struct template, 只要自己写一个
函数
funcc 作为objective function就可以。 可是一直没有明白 class T 应该设成什么

比如说
double funcc(double a);
这时 class T 应写成什么呢? 多谢!
template
struct Funcd {
Doub EPS;
T &func;
Doub f;
Funcd(T &funcc) : EPS(1.0e-8), func(funcc) {}
Doub operator() (VecDoub_I &x)
{
return f=func(x);
}
void df(VecDoub_I &x, VecDoub_O &df)
{
Int n=x.size();
VecDoub xh=x;
Doub fold=f;
for (Int j=
t****t
发帖数: 6806
2
我google了一下好象是numerical recipe里抄来的, 不过你是不是理解不太对
这个struct Funcd似乎是你所需要的functor(你给它一个function, 它把你的function
包装成functor), 然后你把Funcd作为模板参数来instantiate另外的计算函数/类

【在 i*****n 的大作中提到】
: 我想在c++ 里做optimization, 在网上找到如下的struct template, 只要自己写一个
: 函数
: funcc 作为objective function就可以。 可是一直没有明白 class T 应该设成什么
: 。
: 比如说
: double funcc(double a);
: 这时 class T 应写成什么呢? 多谢!
: template
: struct Funcd {
: Doub EPS;

r*********r
发帖数: 3195
3
T 是 double (double)
i*****n
发帖数: 36
4
多谢, 可能我还是不太理解。 以下我尝试 min (x-1)^2 用dfpmin (是使用quasi
newton), 带入参数 Funcd(tryfun), 却无法compile, which
says "cannot convert from 'double (__cdecl *)(double &)' to 'Funcd'"
有解决的方法么?
#include
#include
#include
#include
#include
using namespace std;
typedef double Doub;
typedef int Int;
typedef vector VecDoub, VecDoub_I, VecDoub_O, VecDoub_IO;
template
void dfpmin(VecDoub_IO &p, const Doub gtol, Int &iter, Doub &f

【在 r*********r 的大作中提到】
: T 是 double (double)
r*********r
发帖数: 3195
5
那么多 type matching 错误, 怎么可能编译得过去.
i*****n
发帖数: 36
6
除了 Funcd(tryfun)); type matching 外还有哪些问题呢? 多谢!

【在 r*********r 的大作中提到】
: 那么多 type matching 错误, 怎么可能编译得过去.
i*****n
发帖数: 36
7
抱歉, 有些别的bug, 把 x的 type 从 double 改成 vector
#include
#include
#include
#include
#include
using namespace std;
typedef double Doub;
typedef int Int;
typedef vector VecDoub, VecDoub_I, VecDoub_O, VecDoub_IO;
template
void dfpmin(VecDoub_IO &p, const Doub gtol, Int &iter, Doub &fret, T
&funcd)
{
const Int ITMAX=200;
const Doub EPS=numeric_limits::epsilon();
const Doub TOLX=4*EPS,STPMX=100.0;
Bool check;

【在 r*********r 的大作中提到】
: 那么多 type matching 错误, 怎么可能编译得过去.
1 (共1页)
进入Programming版参与讨论
相关主题
ask a question about struct in C programming[合集] 大家看看我这个C++ STL Functor那里写错了
pointer to function请教C++中的unordered_set
typedefhaskell 真是逆天, 各种特殊符号都有特殊用途
typedef struct的问题haskell 真是逆天, 各种特殊符号都有特殊用途
sizeof()的问题c++ typedef 一问
一个C++ template的问题how to initialize this struct.
请教一个C++ typedef的问题为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?
用STL map的时候怎么自己定义大小比较的关系请教函数 INIT 怎么能free memory
相关话题的讨论汇总
话题: vecdoub话题: doub话题: double话题: int话题: funcd