當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ isgreater()用法及代碼示例


在C++中,isgreater()是用於數學計算的預定義函數。 math.h是各種數學函數所需的頭文件。

isgreater()函數,用於檢查賦予該函數的第一個參數是否大於賦予該函數的第二個參數。表示如果a是第一個參數,b是第二個參數,那麽它將檢查a> b是否。


用法:

bool isgreater(a, b)
參數:
a, b => These two are the parameters whose value we want to compare.
Result:
The function will return the true if a>b else it returns false.

Error: 
No error occurs with this function.
Exception: 
If a or b or both is NaN,
then the function raised an exception and return false(0).
// CPP code to illustrate 
// the exception of function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Take any values 
    int a = 5; 
    double f1 = nan("1"); 
    bool result; 
  
    // Since f1 value is NaN so 
    // with any value of a, the function 
    // always return false(0) 
    result = isgreater(f1, a); 
    cout << f1 << " isgreater than " << a 
         << ":" << result; 
  
    return 0; 
}

輸出:

nan isgreater than 5:0

    例:

  • 程序:
    // CPP code to illustrate 
    // the use of isgreater function 
    #include <bits/stdc++.h> 
    using namespace std; 
      
    int main() 
    { 
        // Take two any values 
        int a, b; 
        bool result; 
        a = 5; 
        b = 8; 
      
        // Since 'a' is not greater 
        // than 'b' so answer 
        // is false(0) 
        result = isgreater(a, b); 
        cout << a << " isgreater than " << b 
             << ":" << result << endl; 
      
        char x = 'd'; 
      
        // Since 'd' ascii value is greater 
        // than b vaiable so answer 
        // is true(1) 
        result = isgreater(x, b); 
        cout << x << " isgreater than " << b 
             << ":" << result; 
      
        return 0; 
    }

    輸出:

    5 isgreater than 8:0
    d isgreater than 8:1
    
  • 注意:使用此函數,您還可以將任何數據類型與任何其他數據類型進行比較。

    應用

  • 此函數可用於任何基於比較的排序算法。讓我們在冒泡排序中使用它:
    // CPP code to illustrate the  
    // use of isgreaterequal function 
    #include <bits/stdc++.h> 
    using namespace std; 
      
    int main() 
    { 
        // taking inputs 
        int arr[] = { 5, 2, 8, 3, 4 }; 
        int n = sizeof(arr) / sizeof(arr[0]); 
      
        for (int i = 0; i < n - 1; i++)  
        { 
            for (int j = 0; j < n - i - 1; j++)  
            { 
                if (isgreater(arr[j], arr[j + 1]))  
                { 
                    int k = arr[j]; 
                    arr[j] = arr[j + 1]; 
                    arr[j + 1] = k; 
                } 
            } 
        } 
      
        cout << "Sorted array:"; 
        for (int i = 0; i < n; i++) { 
            cout << arr[i] << ", "; 
        } 
        return 0; 
    }

    輸出:

    Sorted array:2, 3, 4, 5, 8, 
    


相關用法


注:本文由純淨天空篩選整理自AKASH GUPTA 6大神的英文原創作品 isgreater() in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。