📜  C ++ 20中的midpoint()与示例

📅  最后修改于: 2021-05-31 19:52:16             🧑  作者: Mango

函数midpoint()计算整数,浮点数或指针a和b的中点。

头文件:


参数:此函数接受两种数据类型,例如整数,浮点数和指针值。

返回:

它返回给定数据类型的中点。

该函数实现的Alogrithm

  • a和b的总和的一半而没有任何溢出。与(a + b)/ 2相同。
  • 如果a和b为整数类型,并且总和为奇数,则将结果四舍五入为a。
  • 如果a和b是浮点类型,则最多发生一次不精确的运算(四舍五入为零)。
  • 如果a和b指向 x [i]x [j]分别是同一数组对象x (出于指针算术的目的),结果将是指向x [i +(j – i)/ 2]的指针(或等效地x [ std :: midpoint(i,j)])处的除法取整为零。如果a和b没有指向同一数组对象的元素,则该行为是不确定的。

使用midpoint()函数

  1. 使用midpoint()的两个整数的中点:下面是C++程序,使用midpoint()演示两个整数的中点
    C++
    // C++ program to demonstrate the
    // midpoint function
    #include 
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // a and b both of integer type
        int a = 3;
        int b = 6;
      
        cout << "midpoint(" << a << ", "
             << b << "): "
             << midpoint(a, b) << endl;
      
        a = 6;
        b = 3;
        cout << "midpoint(" << a
             << ", " << b << "): "
             << midpoint(a, b) << endl;
      
        return 0;
    }


    C++
    // C++ program for the above approach
    #include 
    #include 
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // a stores maximum storable
        // value of integer
        int a = INT_MAX;
      
        // b stores maximum storable
        // value of integer - 2
        int b = INT_MAX - 2;
      
        cout << "a: " << a << endl
             << "b: " << b << endl
             << "Incorrect (overflow"
             << " and wrapping): "
             << (a + b) / 2 << endl
             << "Correct: "
             << midpoint(a, b) << "\n\n";
      
        return 0;
    }


    C++
    // C++ program for the above approach
    #include 
    #include 
      
    using namespace std;
      
    int main()
    {
        // x and y both floating type
        float x = 6.56;
        float y = 7.23;
      
        cout << "midpoint(" << x
             << ", " << y << "): "
             << midpoint(x, y) << endl;
      
        x = 2.0;
        y = 3.0;
      
        cout << "midpoint(" << x
             << ", " << y << "): "
             << midpoint(x, y)
             << endl
             << endl;
    }


    C++
    // C++ program for the above approach
    #include 
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // str is a character array
        char str[] = "GeeksforGeeks";
      
        // str1 is pointing to the
        // 5-th element in str
        char* str1 = &str[4];
      
        // str2 is pointing to the
        // 10-th element in str
        char* str2 = &str[9];
      
        // str1 and str2 is pointing same
        // object str therefore, we can
        // logically compute and point a
        // middle element in str
        cout << "midpoint('" << *str1
             << "', '" << *str2 << "'): '"
             << *midpoint(str1, str2)
             << "'" << endl;
      
        cout << "midpoint('" << *str2
             << "', '" << *str1 << "'): '"
             << *midpoint(str2, str1)
             << "'" << endl;
    }


    输出:

  2. midpoint()可以处理类似溢出的情况:下面是C++程序演示如何使用midpoint()处理溢出:

    C++

    // C++ program for the above approach
    #include 
    #include 
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // a stores maximum storable
        // value of integer
        int a = INT_MAX;
      
        // b stores maximum storable
        // value of integer - 2
        int b = INT_MAX - 2;
      
        cout << "a: " << a << endl
             << "b: " << b << endl
             << "Incorrect (overflow"
             << " and wrapping): "
             << (a + b) / 2 << endl
             << "Correct: "
             << midpoint(a, b) << "\n\n";
      
        return 0;
    }
    

    输出:

  3. 使用midpoint()的两个浮点数的中点:下面是C++程序,用于演示如何使用midpoint()查找两个浮点数的中点

    C++

    // C++ program for the above approach
    #include 
    #include 
      
    using namespace std;
      
    int main()
    {
        // x and y both floating type
        float x = 6.56;
        float y = 7.23;
      
        cout << "midpoint(" << x
             << ", " << y << "): "
             << midpoint(x, y) << endl;
      
        x = 2.0;
        y = 3.0;
      
        cout << "midpoint(" << x
             << ", " << y << "): "
             << midpoint(x, y)
             << endl
             << endl;
    }
    

    输出:

  4. 下面是C++程序来演示如何找到两个指针使用中点()指向同一个对象的中间点两个指针使用中点()指向同一个对象之间的中间点

    C++

    // C++ program for the above approach
    #include 
    #include 
    using namespace std;
      
    // Driver Code
    int main()
    {
        // str is a character array
        char str[] = "GeeksforGeeks";
      
        // str1 is pointing to the
        // 5-th element in str
        char* str1 = &str[4];
      
        // str2 is pointing to the
        // 10-th element in str
        char* str2 = &str[9];
      
        // str1 and str2 is pointing same
        // object str therefore, we can
        // logically compute and point a
        // middle element in str
        cout << "midpoint('" << *str1
             << "', '" << *str2 << "'): '"
             << *midpoint(str1, str2)
             << "'" << endl;
      
        cout << "midpoint('" << *str2
             << "', '" << *str1 << "'): '"
             << *midpoint(str2, str1)
             << "'" << endl;
    }
    

    输出:

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