gpt4 book ai didi

c++ - 通过配置文件在运行时选择变量类型

转载 作者:行者123 更新时间:2023-11-30 03:36:06 25 4
gpt4 key购买 nike

我看到了有关在运行时动态选择变量类型的答案( thisthis ,以及从那里开始的链接),但是,即使有些可能让我有点头疼(相当C++ 的新手),有没有办法从配置文件中读取变量类型并在运行时使用它?

比如config中可能有type=double作为一行,可以通过程序的设置改为type=long double,并使用无需重新启动应用程序的变量类型。


我需要计算一些多项式的根。有些,即使是小订单,也需要很大的精度,如果是高订单,就需要更大的精度。不过,对于足够小的数字,我可以取消 doublelong double,但随着我变得更高,我需要 MPFRC++(这就是我目前正在唱的)。我想避免减速(即使它对于较小的订单来说并不那么明显)但只在需要时才能够使用高精度。

最佳答案

您可能会使用的是 Factory Pattern连同 Strategy Pattern和适当的接口(interface)。沿线的东西:

struct IPolyRootSolver {
virtual PolyRoot calcRoot(const Polynom& poly) const = 0;
virtual ~IPolyRootSolver () {}
};

class DoublePolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on double precision
}
};

class LongDoublePolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on long double precision
}
};

class MPFRCPolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on MPFRC++ precision
}
};

class RootSolverFactory {
public:
RootSolverFactory(const std::string configFile) {
// Read config file and install a mechanism to watch for changes
}

std::unique_ptr<IPolyRootSolver> getConfiguredPolyRootSolver() {

if(config file contains type = double) {
return make_unique<IPolyRootSolver>(new DoublePolyRootSolver());
}
else if(config file contains type = long double) {
return make_unique<IPolyRootSolver>(new LongDoublePolyRootSolver());
}
else if(config file contains type = MPFRC) {
return make_unique<IPolyRootSolver>(new MPFRCPolyRootSolver ());
}
else {
// Handle the default case
}
};

希望你明白我的意思。


如评论中所述,您还可以使用命名空间中的独立函数,而不是上述抽象接口(interface)解决方案:

namespace PolyRootDoublePrecision {
PolyRoot calcRoot(const Polynom&);
}

namespace PolyRootLongDoublePrecision {
PolyRoot calcRoot(const Polynom&);
}

namespace PolyRootMPFRCPrecision {
PolyRoot calcRoot(const Polynom&);
}

class RootSolverFactory {
public:
RootSolverFactory(const std::string configFile) {
// Read config file and install a mechanism to watch for changes
}

std::function<PolyRoot (const Polynom&)> getConfiguredPolyRootSolver() {

if(config file contains type = double) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootDoublePrecision::calcRoot);
}
else if(config file contains type = long double) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootLongDoublePrecision::calcRoot);
}
else if(config file contains type = MPFRC) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootMPFRCPrecision::calcRoot);
}
else {
// Handle the default case
}
};

除了使用配置文件外,您还可以考虑从其他条件中选择要使用的策略作为配置文件。

例如对于您的情况,多项式表达式的复杂性(即子项的数量)似乎在选择要使用的最佳策略方面起着至关重要的作用。所以你可以写一些代码,比如

    std::function<PolyRoot (const Polynom&)> getPolyRootSolver(const Polynom& polynom) {

if(polynom.complexity() < 7) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootDoublePrecision::calcRoot);
}
else if(polynom.complexity() >= 7 &&
polynom.complexity() < 50) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootLongDoublePrecision::calcRoot);
}
else if(polynom.complexity() >= 50) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootMPFRCPrecision::calcRoot);
}
};

关于c++ - 通过配置文件在运行时选择变量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com