📜  带有Boost库的高级C++

📅  最后修改于: 2021-05-30 13:51:22             🧑  作者: Mango

Boost库旨在广泛有用,并且可以在广泛的应用程序中使用。例如,它们有助于处理C++中超出long long,long double数据类型(2 64)范围的大量数字。

安装

  • 请参阅此文章以安装boost。我们可以下载zip文件。之后,我们只需要提取整个gcc的指定文件夹中的内容,否则我们可以通过命令提示符轻松地完成提取。
  • 如果您有一个代码块编译器,那么这是一篇很好的文章,有完整的说明。

应用范例

我们可以在竞争性编程中有效地使用该库,但是在此之前,我们必须确保您的在线裁判必须支持Boost。这里有一些很酷的技巧,您可以使用:

  1. 大整数数据类型根据您的要求,我们可以使用int128_t,int256_t,int512_t或int1024_t数据类型。通过使用这些,我们可以轻松实现高达1024的精度。
    查找大量产品的C++下面的实现代码。
    #include 
    using namespace boost::multiprecision;
    using namespace std;
      
    int128_t boost_product(long long A, long long B)
    {
        int128_t ans = (int128_t) A * B;
        return ans;
    }
      
    int main()
    {
        long long first = 98745636214564698;
        long long second=7459874565236544789;
        cout << "Product of "<< first << " * "
             << second << " = \n"
             << boost_product(first,second) ;
        return 0;
    }
    

    输出:

    Product of 98745636214564698 * 7459874565236544789 =
    736630060025131838840151335215258722
    
  2. 任意精度数据类型:如果不确定将来需要多少精度,可以在cpp_int数据类型的帮助下使用任何精度。它会在运行时自动转换所需的精度。
    下面的C++代码实现用于查找30的阶乘。
    #include 
    using namespace boost::multiprecision;
    using namespace std;
      
    cpp_int boost_factorial(int num)
    {
        cpp_int fact = 1;
        for (int i=num; i>1; --i)    
            fact *= i;
        return fact;
    }
      
    int main()
    {  
        int num=30;
        cout << "Factorial of " << num << " = " 
             << boost_factorial(num) ;
        return 0;
    }
    

    输出:

    Factorial of 30 = 265252859812191058636308480000000
  3. 多精度浮点数:使用Boost多精度浮点数,我们可以分别使用cpp_float_50和cpp_dec_float_100达到高达50和100十进制的精度。
    下面是C++代码,通过使用float,decimal和cpp_float_50类型来计算不同精度的圆的面积。

    #include
    #include 
    #include 
      
    using boost::multiprecision::cpp_dec_float_50;
      
    using namespace std;
      
    template
    inline T area_of_a_circle(T r)
    {  
       // pi represent predefined constant having value
       // 3.1415926535897932384...
       using boost::math::constants::pi;
       return pi() * r * r;
    }
      
    int main()
    {
        float radius_f = 123.0/ 100;
        float area_f = area_of_a_circle(radius_f);
      
        double radius_d = 123.0 / 100;
        double area_d = area_of_a_circle(radius_d);
      
        cpp_dec_float_50 r_mp = 123.0 / 100;
        cpp_dec_float_50 area_mp = area_of_a_circle(r_mp);
      
        // numeric_limits::digits10 represent the number
        // of decimal digits that can be held of particular
        // data type without any loss.
          
        // Area by using float data type
        cout << "Float: "
             << setprecision(numeric_limits::digits10)
             << area_f << endl;
      
        // Area by using double data type
        cout << "Double: "
             <::digits10)
             << area_d << endl;
      
        // Area by using Boost Multiprecision 
        cout << "Boost Multiprecision: "
             << setprecision(numeric_limits::digits10)
             << area_mp << endl;
        return 0;
    }
    

    输出:

    Float: 4.75292
    Double: 4.752915525616
    Boost Multiprecision: 4.7529155256159980531876290929438093413108253981451
    
    

参考: http://www.boost.org/doc/libs/1_61_0/libs/multiprecision/doc/html/index.html

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”