📜  C++中的is_arithmetic模板

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

C++ STL的std :: is_arithmetic模板用于检查给定类型是否为算术类型。算术类型是指整数类型或浮点类型。它返回一个显示相同值的布尔值。

语法

template  struct is_arithmetic;

参数:该模板接受单个参数T(Trait类),以检查T是否为算术类型。

返回值:该模板返回一个布尔值,如下所示:

  • True :如果类型是算术运算符。
  • False :如果类型是非算术类型。

下面的程序说明了C++ STL中的std :: is_arithmetic模板:

程序1

// C++ program to illustrate
// is_arithmetic template
  
#include 
#include 
using namespace std;
  
// main program
class GFG {
};
  
int main()
{
    cout << boolalpha;
    cout << "is_arithmetic:" << '\n';
    cout << "GFG: "
         << is_arithmetic::value << '\n';
    cout << "bool: "
         << is_arithmetic::value << '\n';
    cout << "long: "
         << is_arithmetic::value << '\n';
    cout << "short: "
         << is_arithmetic::value << '\n';
    return 0;
}
输出:
is_arithmetic:
GFG: false
bool: true
long: true
short: true

程序2

// C++ program to illustrate
// is_arithmetic template
  
#include 
#include 
using namespace std;
  
// main program
class GFG {
};
  
int main()
{
    cout << boolalpha;
    cout << "is_arithmetic:" << '\n';
    cout << "GFG: "
         << is_arithmetic::value << '\n';
    cout << "int: "
         << is_arithmetic::value << '\n';
    cout << "int const: "
         << is_arithmetic::value << '\n';
    cout << "int &: "
         << is_arithmetic::value << '\n';
    cout << "int *: "
         << is_arithmetic::value << '\n';
    cout << "long int: "
         << is_arithmetic::value << '\n';
  
    return 0;
}
输出:
is_arithmetic:
GFG: false
int: true
int const: true
int &: false
int *: false
long int: true

程序3

// C++ program to illustrate
// is_arithmetic template
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_arithmetic:" << '\n';
    cout << "float: "
         << is_arithmetic::value << '\n';
    cout << "float const: "
         << is_arithmetic::value << '\n';
    cout << "float &: "
         << is_arithmetic::value << '\n';
    cout << "float *: "
         << is_arithmetic::value << '\n';
    cout << "double: "
         << is_arithmetic::value << '\n';
    cout << "double const: "
         << is_arithmetic::value << '\n';
    cout << "double &: "
         << is_arithmetic::value << '\n';
    cout << "double *: "
         << is_arithmetic::value << '\n';
  
    return 0;
}
输出:
is_arithmetic:
float: true
float const: true
float &: false
float *: false
double: true
double const: true
double &: false
double *: false

计划4

// C++ program to illustrate
// is_arithmetic template
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_arithmetic:" << '\n';
    cout << "char: "
         << is_arithmetic::value << '\n';
    cout << "char const: "
         << is_arithmetic::value << '\n';
    cout << "char &: "
         << is_arithmetic::value << '\n';
    cout << "char *: "
         << is_arithmetic::value << '\n';
  
    return 0;
}
输出:
is_arithmetic:
char: true
char const: true
char &: false
char *: false
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”