简体   繁体   English

C ++的两个函数除一行外都相同

[英]c++ two functions do the same except one line

I dont want to duplicate my code, but I have 2 functions which do the same except 1 line. 我不想重复我的代码,但是我有2个函数,除了1行外,它们的功能相同。 I do want separate functions that will call a private function, that might be a template and will solve my duplicate code problem. 我确实想要单独的函数来调用私有函数,该函数可能是模板,并且可以解决我的重复代码问题。

double EuclideanDistance::calculateDistance(const Point &p1, const Point &p2) const
{
    if (p1.getDimension()!=p2.getDimension())
        return 0;
    double sum = 0.0;
    for( size_t i = 0; i < p1.getDimension(); i++)
    {
        sum += pow( p1[i] - p2[i], 2.0);
    }
    return sqrt(sum);
}

double EuclideanDistance::calculateWeightedDistance(const Point &p1, const Point &p2, const double *weights) const
{
    if (p1.getDimension()!=p2.getDimension())
        return 0;
    double sum = 0.0;
    for( size_t i = 0; i < p1.getDimension(); i++)
    {
        sum += pow( ((p1[i] - p2[i])*weights[i]), 2.0);//here is the difference
    }
    return sqrt(sum);
}

what do you suggest? 你有什么建议?

Implement only calculateWeightedDistance fully. 完全仅实现calculateWeightedDistance and for calculateDistance call calculateWeightedDistance with weight 1.0 并为calculateDistance调用权重为1.0 calculateWeightedDistance

What about the following? 那下面呢? If you invoke the function with the third parameter nullptr , then the behaviour of the first version is executed. 如果使用第三个参数nullptr调用该函数,则将执行第一个版本的行为。

double EuclideanDistance::calculateWeightedDistance(const Point& p1, const Point& p2, const double* weights) const {
    if (p1.getDimension()!=p2.getDimension())
        return 0;

    double sum = 0.0;
    for( size_t i = 0; i < p1.getDimension(); i++)
    {
        const auto distance_d = (p1[i] - p2[i]) * 
                                (weights != nullptr ? weights[i] : 1.0);
        sum += pow(distance_d, 2.0);
    }
    return sqrt(sum);
}

You may factorize this way: 您可以将这种方式分解:

template <typename F, typename Ts...>
double calculateDistanceImpl(F&& f, const Point &p1, const Point &p2, Ts&&... args)
{
    if (p1.getDimension()!=p2.getDimension())
        return 0;

    double sum = 0.0;
    for( size_t i = 0; i != p1.getDimension(); i++)
    {
        sum += f(p1[i], p2[i], args[i]...);
    }
    return sqrt(sum);
}


double EuclideanDistance::calculateDistance(const Point &p1, const Point &p2) const
{
    return calculateDistanceImpl(
        [](auto&& p1, auto&& p2){ return pow(p1 - p2, 2.0);},
        p1, p2);
}

double EuclideanDistance::calculateWeightedDistance(const Point &p1,
                                                    const Point &p2,
                                                    const double *weights) const
{
    return calculateDistanceImpl(
        [](auto&& p1, auto&& p2, double weight){ return pow((p1 - p2) * weight, 2);},
        p1, p2, weights);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中定义具有相同签名的两个函数 - Define two functions with the same signature in C++ C++:对于两个带有 do-while 循环的不同函数,为什么 x+=y 在一个 function 中给出与 x=x+y 相同的结果,而在另一个中却不是? - C++: For two different functions with do-while loops, why does x+=y give the same result as x=x+y in one function but not the other? 如何在C ++中使用相同的实现正确表达两个派生类函数? - How do I properly express two derived class functions with the same implementation in C++? 这些清晰的函数在C ++中的作用相同吗 - Do these clear functions do the same thing in C++ 在同一fstream对象C ++上使用getline的两个函数 - Two functions working with getline on same fstream object c++ 使两个函数在C ++中具有相同的内存地址 - Make two functions have the same memory address in C++ 如何以相同的参数自动运行一百个 C++ 函数? - How to auto run one hundred C++ functions with same parameters? C ++初学者:尝试将两个函数合并为一个 - C++ beginner: Trying to incorporate two functions into one 如何使用条件调用两个 C++ 模板函数之一 - How to call one of two C++ template functions with condition 在C ++中选择两个纯虚函数之一 - Choosing one of two pure virtual functions in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM