GCD的多个参数

时间:2018-03-01 07:57:04

标签: c++ c++11 operator-overloading greatest-common-divisor generalization

我的目标是概括C ++中std __gcd()标题中的<algorithm>。它应该能够调用std::vector数组的一系列值。

直接传递参数时,通用模板__gcd()可以正常工作。如下所示:

math::GCD(1,2,58,54);

但是,当我尝试传递一系列向量值时(代码如下),它显示以下错误:

D:\Programming\C++\CPP Programs\My Test\My Test.cpp|33|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')|

我不知道如何重载operator<<或在此模板/程序中包含重载函数。(假设错误是由于operator<<的重载错过了)

namespace math
{

    template <typename M, typename N>
    constexpr auto GCD(const M& m, const N& n) {
        return __gcd(m, n);
    }

    template <typename M, typename ...Rest>
    constexpr auto GCD(const M& first, const Rest&... rest) {
        return __gcd(first, GCD(rest...));
    }
}

int main()
{
    vector<int> vec={1,2,58,54,102,2,37,13,8};
    for(auto i=0; i<vec.size()-3; ++i)
        cout<<math::GCD(vec.begin()+i, vec.begin()+i+4)<<endl;
    return 0;
}

任何人都可以帮助我重载operator<<或(如果不是这样的话)来查找错误吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我终于做到了。感谢@Incomputable的最后评论。但是,我不得不在新模板(用于迭代器)和我们在问题中使用math名称进行分离。如下所示:

#include <iostream>
#include <vector>

template<typename T64> constexpr  T64 calcGCD(T64 a, T64 b)
{
    return (a==0 && b!=0) ? b: a;
    while(b)
    {
        T64 t = a % b;
        b = a;
        a = t;
    }
    return a;
}

template <typename Iterator> constexpr auto GCD(Iterator first, Iterator last)
{
    auto itrFirst = first;
    auto itrEnd   = last;
    int Size      = std::distance(itrFirst,itrEnd);
    int Result = 0;

    if(Size >=2 )
    {
        int A       = *itrFirst;
        std::advance (itrFirst,1);
        int B       = *itrFirst;

        Result      = calcGCD(A,B);
        std::advance (itrFirst,1);

        for(int i = 2; i<Size; ++i)
        {
            A       = *itrFirst;
            Result  = calcGCD(Result,A);
            std::advance (itrFirst,1);
        }
    }
    else   return *itrFirst;
    return Result;
}

namespace math
{
    template <typename M, typename N>
    constexpr auto GCD(const M& m, const N& n)
    { return calcGCD(m, n); }

    template <typename M, typename ...Rest>
    constexpr auto GCD(const M& first, const Rest&... rest)
    { return calcGCD(first, GCD(rest...));  }
}


int main()
{
    std::vector<int> vec={1,2,58,54,102,2,37,13,8};

    std::cout<<"GCD from Iterator Template     : ";
    for(unsigned int i=0; i<vec.size()-3; ++i)
      std::cout<<GCD(vec.begin()+i, vec.begin()+i+4)<<" ";

    std::cout<<std::endl;

    std::cout<<"GCD from non-iterator Templates: ";
    std::cout<<math::GCD(1,2,58,54)<<" ";
    std::cout<<math::GCD(2,58,54,102)<<" ";
    std::cout<<math::GCD(58,54,102,2)<<" ";
    std::cout<<math::GCD(54,102,2,37)<<" ";
    std::cout<<math::GCD(102,2,37,13)<<" ";
    std::cout<<math::GCD(2,37,13,8)<<" ";   

    return 0;
}

它按预期提供所需的输出。但是,我没有测试任何其他情况。

Out Put:

GCD from Iterator Template     : 1 2 58 54 102 2 
GCD from non-iterator Templates: 1 2 58 54 102 2