【问题标题】:Implementation of the in-built __gcd method in C++在 C++ 中实现内置的 __gcd 方法
【发布时间】:2014-07-27 13:21:18
【问题描述】:

stl-algorithm 库中内置的 __gcd 方法在其实现中是否使用了欧几里得算法?

【问题讨论】:

  • 您询问的是例程的实现细节,该例程本身就是特定版本库的实现细节。 __gcd 不是标准函数,如果它是标准的,它的精确算法可能不会。
  • 具体是哪个编译器的实现?

标签: c++ greatest-common-divisor


【解决方案1】:

源代码好像是

  /**
   *  This is a helper function for the rotate algorithm specialized on RAIs.
   *  It returns the greatest common divisor of two integer values.
   */
  template<typename _EuclideanRingElement>
    _EuclideanRingElement
    __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
    {
      while (__n != 0)
        {
          _EuclideanRingElement __t = __m % __n;
          __m = __n;
          __n = __t;
        }
      return __m;
    }

所以是的,它确实使用了欧几里得算法。

编辑:我稍微误读了这个问题,这是 g++ 4.9.1 标题中的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多