- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个大项目,我遇到了一个问题,可以简述如下:
我有一个临时创建的类,用于处理和修改一些数据(我们称它为“worker”)。现在我有两个 worker 和两种相应的数据格式。数据数组可以包含混合数据,如何让我的程序自动决定它应该创建哪个工作类并用于数据处理?如何以最佳方式做到这一点?
为了说明这个问题,我编写了与我的项目类似的小示例程序。
#include <iostream>
#include <vector>
using namespace std;
const int NInputs = 10;
struct TOutput {
int i;
};
class TProcess {
public:
TProcess( const vector<TInput>& i ){ fInput = i; }
void Run();
void GetOutput( TOutput& o ) { o = fOutput; }
private:
vector<TInput> fInput;
TOutput fOutput;
};
#if 0
struct TInput {
int i;
};
class TWorker{
public:
void Init( int i ) { fResult = i; }
void Add( int i ) { fResult += i; }
int Result() { return fResult; }
private:
int fResult;
};
#else
struct TInput {
int i;
};
class TWorker {
public:
void Init( int i ) { fResult = i; }
void Add( int i ) { fResult ^= i; }
int Result() { return fResult; }
private:
int fResult;
};
#endif
void TProcess::Run() {
TWorker worker;
worker.Init(0);
for( int i = 0; i < fInput.size(); ++i )
worker.Add(fInput[i].i);
fOutput.i = worker.Result();
}
int main() {
vector<TInput> input(NInputs);
for ( int i = 0; i < NInputs; i++ ) {
input[i].i = i;
}
TProcess proc(input);
proc.Run();
TOutput output;
proc.GetOutput(output);
cout << output.i << endl;
}
这个例子很简单,但这并不意味着可以将它简单地转化为一个功能---它对应于一个大项目。因此不可能:
那么如何修改它,使其变成这样:
// TODO: TProcess declaration
struct TInput1 {
int i;
};
class TWorker1{
public:
void Init( TInput1 i ) { fResult = i; }
void Add( TInput1 i ) { fResult += i.i; }
int Result() { return fResult; }
private:
int fResult;
};
#else
struct TInput2 {
int i;
};
class TWorker2 {
public:
void Init( TInput2 i ) { fResult = i.i; }
void Add( TInput2 i ) { fResult ^= i.i; }
int Result() { return fResult; }
private:
int fResult;
};
void TProcess::Run() {
for( int i = 0; i < fInput.size(); ++i ) {
// TODO: choose and create a worker
worker.Add(fInput[i].i);
// TODO: get and save result
}
fOutput.i = worker.Result();
}
int main() {
vector<TInputBase> input(NInputs);
// TODO: fill input
TProcess proc(input);
proc.Run();
TOutput output;
proc.GetOutput(output);
cout << output.i << endl;
}
我最初的想法是使用基本的类和模板函数,但是没有模板虚函数...
最佳答案
您对 vector<TInputBase>
的想法是正确的第二个示例中的声明——您需要为所有输入设置一个公共(public)基类,对于所有工作人员也类似:
class TInput {
}
class TInput1 : public TInput { ... }
class TInput2 : public TInput { ... }
class TWorker {
public:
void Init(TInput *input) = 0;
void Add(TInput *input) = 0;
int Result() = 0;
}
class TWorker1 : public TWorker { ... }
class TWorker2 : public TWorker { ... }
但是请注意,这意味着所有 worker 只能拿 TInput *
作为输入,您需要在每个工作类中转换为正确的输入类。
决定对给定输入使用哪个工作类的最简单方法是询问输入本身!您可以在输入类中有一个虚函数来创建正确类型的 worker:
class TInput {
virtual TWorker *createWorker() = 0;
}
class TInput1 : public TInput {
TWorker *createWorker() {
return new TWorker1();
}
}
class TInput2 : public TInput {
TWorker *createWorker() {
return new TWorker2();
}
}
如果由于某种原因无法做到这一点,您可以使用 typeid
判断输入的类型并创建对应的worker实例。
关于c++ - 自动决定使用哪个类进行数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031746/
解释 我的应用程序基本上使用带有代表建筑物的多边形叠加层的 map View ,以及注释。为此,它导入了一个名为 Annotation 的自定义类,该类在点击注释时处理弹出详细信息,这意味着它存储建筑
我有一个数据处理问题,我想计算两支球队在不同比赛中的进球数差异。数据如下所示: matchId teamId eventSec 1 2799331 6718 443.55984
如下所示: ? 1
我想知道 cocoa 是否有默认的数据存储方式。如果是,那是什么?我的意思是 Rails 默认使用 sqlite... 此外,我正在寻找有关如何使用它的教程...例如获取数据并将其显示到 ListVi
我正在使用 HTML5 创建在线游戏。我将使用 JSON 字符串通过 Websockets 通信数据,因此典型的字符串将包含被调用的操作以及随之而来的数据: {action: "chat", user
我需要按特定列对一组 csv 行进行分组,并对每个组进行一些处理。 JavaRDD lines = sc.textFile ("somefile
我有一个情况: 基本上有 3 个模块,分别命名为“A”、“B”、“C”。每个模块都涉及多线程。 模块“A”获取高速数据(20ms)并发送。模块“B”的一个线程启动。 模块“B”提取相关数据并执行一些位
我正在处理有关城镇和城镇内区域的 MySQL 数据库中的一些数据。 数据库看起来像这样 ID | NAME 1 | Manchester 2 | Manchester/North 3 | Man
当我注册用户时,我得到一个状态代码 200 和一个 token :“”返回 JSON。如果用户已经存在,那么我会得到状态代码 200 和 html 响应而不是 JSON。我应该如何处理这个问题。提前致
我有一个应用程序,我从网络上下载大量资源,并对每个资源进行一些处理。我不希望这项工作发生在主线程上,但它非常轻量级且优先级较低,因此所有这些工作都可以真正发生在同一个共享工作线程上。这似乎是一件好事,
我目前正在与一家小公司合作,该公司将其所有应用程序数据存储在 AWS Redshift 集群中。我的任务是对该 Redshift 集群中的数据进行一些数据处理和机器学习。 我需要做的第一个任务是根据一
简介 有些 post 的请求参数是 json 格式的,这个前面发送post 请求里面提到过,需要导入 json模块处理。现在企业公司一般常见的接口因为json数据容易处理,所以绝大多数返回数据也是
1.数组的处理: 1.1 数组的创建和初始化: 1.arrary()函数创建数组,默认情况下0元素是数组的第一个元素, count()和sizeof()函数获得数
我正在尝试将 CKEditor 与 AngularJS 结合使用,用于具有数据绑定(bind)的 WYSIWYG 编辑器,一切似乎都运行良好。极端的可配置性对我们的需求匹配有很大帮助。 我们现在面临表
我正在对负样本和正样本进行文本二元分类任务,我想包括以下所有内容: 处理数据..(例如标记化) 特征选择,例如 Chi2 应用随机投影,因为我有一个大型稀疏矩阵(n_samples:974,n_fea
数据与我在 Pandas 系列: data = ["1. stock1 (1991)", "3. stock13 (1993)", "5. stock19 (1999)", "89. stock
1.字符串的定义与显示 定义:通过””,''来标志 显示:echo()和print(),但print()具有返回值值,1,而echo()没有,但echo比print()要快,
1.正则表达式基础知识 含义:由普通字符和(a-z)和一些特殊字符组成的字符串模式 功能:有效性验证。 替换文本。 从一个字符串提取一个子字符串。&n
我想知道是否可以在我的 Cost Explorer 中发现这个成本背后的资源,按使用类型分组我可以看到它是数据处理字节,但我不知道哪个资源会消耗这个数量数据的。知道如何在 CloudWatch 上发现
我有一个 Json 字符串。我想从该 Json 字符串中获取值。 这是我的 json 字符串{“纬度”:“22.5712854”},{“经度”:“88.4266847”} 我只需要使用 TSQL 查询
我是一名优秀的程序员,十分优秀!