📜  C++中的is_signed模板

📅  最后修改于: 2021-05-30 12:54:42             🧑  作者: Mango

C++ STL的std :: is_signed模板用于检查类型是否为带符号算术类型。

语法

template  struct is_signed;

参数:该模板包含单个参数T(Trait类),以检查T是否为带符号算术类型。

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

  • True :如果类型是带符号的算术类型。
  • False :如果类型是无符号算术类型。

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

程序1

// C++ program to illustrate
// std::is_signed template
  
#include 
#include 
using namespace std;
  
class gfg {
};
  
enum sam : int {};
enum class raj : int {};
  
int main()
{
    cout << boolalpha;
    cout << "is_signed:" << '\n';
    cout << "int:" << is_signed::value << '\n';
    cout << "gfg:" << is_signed::value << '\n';
    cout << "sam:" << is_signed::value << '\n';
    cout << "raj:" << is_signed::value << '\n';
    return 0;
}
输出:
is_signed:
int:true
gfg:false
sam:false
raj:false

程序2

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

程序3

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