我正在开发一个应用程序,其中我有很多小算法,每个算法由一些代码行代表,所以我想将几行代码存储为函数,但不仅如此,我必须存储每个算法所拥有的一些数据,所以我决定制作一个"算法"上课,其中,我将在一个"变量"存储的功能。所以,我以后可以使用它。
我不知道这是否可能,或者是否有其他方法可以实现。 我认为本地变量或类的私有成员会遇到问题我的算法"是
class Patterns {
private:
double line;
double addPoint(char n) {line += n;}
public:
double addPattern(int m) {
double tmp = 0;
char param;
// some calculations with m
// many calls to addPoint, "algorithm"
tmp += addPoint(param); // param1
tmp += addPoint(param); // param2
tmp += addPoint(param); // param3
tmp += addPoint(param); // param4
return tmp;
}
}
只是一个小样本,我想存储" addPoints()"只在一个函数中,并随时使用,像这样
class Patterns {
private:
double line;
double addPoint(char n) {line += n;}
public:
double addPattern(int m) {
double tmp = 0;
// some calculations with m
/**
* vector of Algorithm class, get any Algorithm, and of that,
* return the stored function, so I can use it as in the above sample
*/
auto fn = vec->back()->getFunction();
tmp += fn(m)
return tmp;
}
}
编辑:此问题包括使用库<functional>
答案 0 :(得分:3)
听起来如何:
#include <vector>
#include <functional>
int test(int a)
{
return a*2;
}
int main()
{
using namespace std;
vector < function<int(int)>> fv;
fv.push_back([](int a) {return a + 5; });
(fv.back())(10);
fv.push_back(test);
(fv.back())(240);
}
对于您的课程,您需要更改function
的模板参数的类型。
编辑(最小class
示例):
class Pattern
{
double addPoint(char n)
{
return n * 99.0;
}
double addPoint2(char n)
{
return n * 188.25;
}
vector < function<double(char)>> funcs;
public:
Pattern()
{
funcs.push_back(std::bind(&Pattern::addPoint, this, placeholders::_1));
funcs.push_back(std::bind(&Pattern::addPoint2, this, placeholders::_1));
}
void Call()
{
cout << (funcs.back())('A');
}
};
int main()
{
Pattern p;
p.Call();
}
但是,如果函数是全局函数或静态函数,则不需要执行bind
。
答案 1 :(得分:2)
使用OOD有多种方法可以解决问题。由于您有许多算法及其各自的数据,因此为此类算法创建类是有意义的。现在,就OOD而言,您可以使用Design Pattern : Strategy。
它是这样的:
为所有算法定义接口
class IAlgorithm
{
public:
virtual int Execute(/*external parameter list on which all algorithm depend.*/) = 0;
};
现在,定义从接口IAlgorithm
继承的不同算法。
class CAlgorithm_Type1 : public IAlgorithm
{
private:
/* all the data members internally and exclusively used by this algorithm*/
public:
int Execute(/*external parameter list on which all algorithm depend.*/);
};
class CAlgorithm_Type2 : public IAlgorithm
{
private:
/* all the data members internally and exclusively used by this algorithm*/
public:
int Execute(/*external parameter list on which all algorithm depend.*/);
};
现在定义这些算法的客户端。
class Patterns
{
private:
double line;
double addPoint(char n) {line += n;}
IAlgorithm *m_pAlgorithm;
public:
SetAlgorithm(IAlgorithm *_pAlgorithm)
{
m_pAlgorithm = _pAlgorithm;
}
double addPattern(int m) {
double tmp = 0;
tmp += m_pAlgorithm->Execute(m);
return tmp;
}
};
现在根据要求,可以有一个算法工厂
class ALGORITHM_LIBRARY
{
public:
enum TYPE
{
ALGORITHM_TYPE1,
ALGORITHM_TYPE2,
TOTAL_ALGORITHMS
};
ALGORITHM_LIBRARY()
{
m_Algorithms[ALGORITHM_LIBRARY::ALGORITHM_TYPE1] = new CAlgorithm_Type1();
m_Algorithms[ALGORITHM_LIBRARY::ALGORITHM_TYPE2] = new CAlgorithm_Type2();
}
~ALGORITHM_LIBRARY()
{
map<ALGORITHM_LIBRARY::TYPE, IAlgorithm*>::iterator it;
for(it = m_Algorithms.begin(); it != m_Algorithms.end(); ++it)
{
delete it->second;
}
}
IAlgorithm* GetAlgorithm(ALGORITHM_LIBRARY::TYPE _enumAlgorithmType)
{
return m_Algorithms[_enumAlgorithmType];
}
private:
map<ALGORITHM_LIBRARY::TYPE, IAlgorithm*> m_Algorithms;
};
ALGORITHM_LIBRARY g_oAlgorithmLibrary;
enum enumAlgorithmType = ALGORITHM_LIBRARY::ALGORITHM_TYPE2;
Pattern Obj;
Obj.SetAlgorithm(g_oAlgorithmLibrary.GetAlgorithm(enumAlgorithmType));
Obj.addPattern(20);