Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

예제가 있는 C++의 ratio_not_equal()


이 기사에서는 C++ STL에서 ratio_not_equaltemplate의 작업, 구문 및 예에 대해 논의합니다.

ratio_not_equal 템플릿이란 무엇입니까?

ratio_not_equal 템플릿은 헤더 파일에 정의된 C++ STL에 내장되어 있습니다.ratio_not_equal은 같지 않은 두 비율을 비교하는 데 사용됩니다. 이 템플릿은 두 개의 매개변수를 허용하고 주어진 비율이 같지 않아야 하는지 여부를 확인합니다. 1/2와 3/9라는 두 가지 비율이 있는 것처럼 동일하지 않으므로 주어진 템플릿에 해당합니다. 이 함수는 두 비율이 같지 않을 때 true를 반환합니다.

따라서 두 비율의 부등식을 확인하려는 경우 전체 논리를 C++로 작성하는 대신 제공된 템플릿을 사용하여 코딩을 더 쉽게 만들 수 있습니다.

구문

template <class ratio1, class ratio2> ratio_not_equal;

매개변수

템플릿은 다음 매개변수를 허용합니다. -

  • 비율1, 비율2 − 이 두 비율이 같지 않은지 확인하려는 두 가지 비율입니다.

반환 값

이 함수는 두 비율이 같지 않으면 true를 반환하고 두 비율이 같으면 false를 반환합니다.

입력

typedef ratio<3, 6> ratio1;
typedef ratio<1, 2> ratio2;
ratio_not_equal<ratio1, ratio2>::value;

출력

false

입력

typedef ratio<3, 9> ratio1;
typedef ratio<1, 2> ratio2;
ratio_not_equal<ratio1, ratio2>::value;

출력

true

예시

#include <iostream>
#include <ratio>
using namespace std;
int main(){
   typedef ratio<2, 5> R_1;
   typedef ratio<1, 3> R_2;
   //check whether ratios are equal or not
   if (ratio_not_equal<R_1, R_2>::value)
      cout<<"Ratio 1 and Ratio 2 aren't equal";
   else
      cout<<"Ratio 1 and Ratio 2 are equal";
   return 0;
}

출력

위 코드를 실행하면 다음 출력이 생성됩니다. -

Ratio 1 and Ratio 2 aren't equal

예시

#include <iostream>
#include <ratio>
using namespace std;
int main(){
   typedef ratio<2, 5> R_1;
   typedef ratio<2, 5> R_2;
   //check whether ratios are equal or not
   if (ratio_not_equal<R_1, R_2>::value)
      cout<<"Ratio 1 and Ratio 2 aren't equal";
   else
      cout<<"Ratio 1 and Ratio 2 are equal";
   return 0;
}

출력

위 코드를 실행하면 다음 출력이 생성됩니다. -

Ratio 1 and Ratio 2 aren equal