在C++中定義分段函數的最佳方式是什麼?定義分段函數(例如多項式)
實施例:
f1(x) if x from [0, 5)
f(x) = f2(x) if x from [5, 10)
f3(x) if x from [10, 20)
我目前的做法是這樣的:
class Function
{
virtual double operator()(double x) = 0;
}
class SomeFun : public Function
{
// implements operator() in a meaningful way
}
class PiecewiseFunction : public Function
{
// holds functions along with the upper bound of the interval
// for which they are defined
// e.g. (5, f1), (10, f2), (20, f3)
std::map< double, Function* > fns;
virtual double operator()(double x)
{
// search for the first upper interval boundary which is greater than x
auto it = fns.lower_bound(x);
// ... and evaluate the underlying function.
return *(it->second)(x);
}
}
這種方法缺乏檢查如果x
是在功能的整體範圍,例如[0,20)在上面的例子,我知道,也許命名不是最好的(Function
與std::function
等等)。
任何想法如何以更聰明的方式做到這一點?該方法使用鍵的屬性在std::map
中排序。這不是關於效率,而是關於乾淨的設計。
切片
不是問題的確切一部分,但在一個評論,切片中提到,在這裏你可以讀到它。
std::map unable to handle polymorphism?
我糾正了這個在我上面的代碼。
if/else語句有什麼問題? – perreal
我不認爲這會看起來更漂亮,是嗎? –
@perreal,如果他有一個分段功能與大量的部分,然後如果語句將比二進制搜索慢......和一團糟。這種方法使構建分段函數變得相對容易。我認爲沃爾瑪墊子應該基本上與所呈現的一致。 –