在C++中,islessgreater()是用於數學計算的預定義函數。 math.h是各種數學函數所需的頭文件。
islessgreater()函數用於檢查賦予該函數的第一個參數是否小於或大於賦予該函數的第二個參數。表示如果a是第一個參數,b是第二個參數,則檢查a> b ||是否一種
用法:
bool islessgreater(a, b)
- a,b =>這兩個是我們要比較其值的參數。
- 如果(a> b或a <b),則該函數將返回true;否則,當它們相等時(a == b),它將返回false。
- 此函數沒有錯誤發生。
- 如果a或b或兩者均為NaN,則該函數引發異常並返回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("2.0"); 
    bool result; 
  
    // Since f1 value is NaN so 
    // with any value of a, the function 
    // always return false(0) 
    result = islessgreater(f1, a); 
    cout << f1 << " islessgreater than " << a 
         << ":" << result; 
  
    return 0; 
}輸出:
nan islessgreater than 5:0
例:
- 程序:
// CPP code to illustrate // the use of islessgreater function #include <bits/stdc++.h> using namespace std; int main() { // Take two any values float a, b; bool result; a = 10.2; b = 8.5; // Since 'a' is not less than 'b' // but greater than 'b' so answer // is true(1) result = islessgreater(a, b); cout << a << " islessgreater than " << b << ":" << result << endl; int x = 2; // Since 'x' is not greater than 'b' // but less than 'a' vaiable so answer // is true(1) result = islessgreater(x, a); cout << x << " islessgreater than " << a << ":" << result << endl; a = 8.5; // Since value of 'a' and 'b' are // equal so answer is false(0) result = islessgreater(a, b); cout << a << " islessgreater than " << b << ":" << result; return 0; }輸出: 10.2 islessgreater than 8.5:1 2 islessgreater than 10.2:1 8.5 islessgreater than 8.5:0 
應用:
此函數可以在各種地方使用,其中之一可以用於線性搜索。
// CPP code to illustrate the 
// use of islessgreater 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]); 
  
    // Lets we want to search for 1 in 
    // the arr array 
    int a = 1; 
    int flag = 0; 
      
    for (int i = 0; i < n; i++)  
    { 
        if (islessgreater(arr[i], a))  
        { 
            flag = 1; 
        } 
    } 
  
    if (flag == 0)  
    { 
        cout << a << " is present in array"; 
    } 
      
    else 
    { 
        cout << a << " is not present in array"; 
    } 
    return 0; 
}輸出:
1 is not present in array
相關用法
注:本文由純淨天空篩選整理自AKASH GUPTA 6大神的英文原創作品 islessgreater() in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
