📜  C ++中的std :: numeric_limits :: max()和std :: numeric_limits :: min()

📅  最后修改于: 2021-05-31 19:29:00             🧑  作者: Mango

std :: numeric_limits :: max()

std :: numeric_limits :: max()函数用于获取可由数字类型T表示的最大有限值。所有算术类型均对类型T有效。

头文件:

#include

模板:

static T max() throw();
static constexpr T max() noexcept;

句法:

std::numeric_limits::max

参数:它接收任何一种数据类型,即T。

返回类型:根据类型T,它返回预定义的宏,是true还是默认T()。一些标准返回值是:

Type T std::numeric_limits::max()
/* non-specialized */ T()
bool TRUE
char CHAR_MAX
signed char SCHAR_MAX
unsigned char UCHAR_MAX
wchar_t WCHAR_MAX
short SHRT_MAX
unsigned short USHRT_MAX
int INT_MAX
unsigned int UINT_MAX
long LONG_MAX
unsigned long ULONG_MAX
long long LLONG_MAX
unsigned long long ULLONG_MAX
float FLT_MAX
double DBL_MAX
long double LDBL_MAX

程序1:

下面是说明函数std :: numeric_limits :: max()的程序该程序的输出是特定于系统的。

C++
// C++ program to illustrate the
// function numeric_limits::max
#include 
#include 
using namespace std;
  
// Driver Code
int main()
{
    cout << "bool: "
         << numeric_limits::max()
         << '\n';
  
    // It returns 127 in ASCII value
    // to print in integer that can
    // be typecast it to int()
    cout << "char: "
         << int(numeric_limits::max())
         << '\n';
  
    cout << "unsigned char: "
         << int(numeric_limits::max())
         << '\n';
  
    cout << "short: "
         << numeric_limits::max()
         << '\n';
  
    cout << "int: " << numeric_limits::max()
         << '\n';
  
    cout << "unsigned int: "
         << numeric_limits::max()
         << '\n';
  
    cout << "long long: "
         << numeric_limits::max()
         << '\n';
  
    cout << "float: "
         << numeric_limits::max()
         << '\n';
  
    cout << "double: "
         << numeric_limits::max()
         << '\n';
  
    cout << "size_t: "
         << numeric_limits::max()
         << '\n';
}


C++
// C++ program to illustrate the
// function numeric_limits::min
#include 
#include 
using namespace std;
  
// Driver Code
int main()
{
    cout << "bool: "
         << numeric_limits::min()
         << '\n';
  
    // numeric_limits:: min()
    // returns 127 in ASCII value in
    // integer that can be typecast
    // to int()
    cout << "char: "
         << int(numeric_limits::min())
         << '\n';
  
    cout << "unsigned char: "
         << int(numeric_limits::min())
         << '\n';
  
    cout << "short: "
         << numeric_limits::min() << '\n';
  
    cout << "int: " << std::numeric_limits::min()
         << '\n';
    cout << "unsigned int: "
         << numeric_limits::min()
         << '\n';
  
    cout << "long long: "
         << numeric_limits::min()
         << '\n';
    cout << "float: "
         << numeric_limits::min()
         << '\n';
  
    cout << "double: "
         << numeric_limits::min()
         << '\n';
  
    cout << "size_t: "
         << numeric_limits::min()
         << '\n';
}


输出:
bool: 1
char: 127
unsigned char: 255
short: 32767
int: 2147483647
unsigned int: 4294967295
long long: 9223372036854775807
float: 3.40282e+38
double: 1.79769e+308
size_t: 18446744073709551615

std :: numeric_limits :: min()

std :: numeric_limits :: min()函数用于获取可由数字类型T表示的最小有限值。所有有界算术类型均对类型T有效。

头文件:

#include

模板:

static T min() throw();
static constexpr T min() noexcept;

句法:

std::numeric_limits::min

参数:它接收任何一种数据类型,即T。

返回类型:返回取决于类型T的预定义宏,为true或默认T()。对于具有非规格化的浮点类型,min返回最小的正规格化值。要查找没有小于浮点数据类型的值的值,请使用numeric_limits :: lowest() 。一些标准返回值是:

Type T std::numeric_limits::min()
/* non-specialized */ T()
bool FALSE
char CHAR_MIN
signed char SCHAR_MIN
unsigned char ​0​
wchar_t WCHAR_MIN
short SHRT_MIN
unsigned short 0
int INT_MIN
unsigned int 0
long LONG_MIN
unsigned long 0
long long LLONG_MIN
unsigned long long ​0​
float FLT_MIN
double DBL_MIN
long double LDBL_MIN

程式2:

下面是说明函数std :: numeric_limits :: min()的程序该程序的输出是特定于系统的。

C++

// C++ program to illustrate the
// function numeric_limits::min
#include 
#include 
using namespace std;
  
// Driver Code
int main()
{
    cout << "bool: "
         << numeric_limits::min()
         << '\n';
  
    // numeric_limits:: min()
    // returns 127 in ASCII value in
    // integer that can be typecast
    // to int()
    cout << "char: "
         << int(numeric_limits::min())
         << '\n';
  
    cout << "unsigned char: "
         << int(numeric_limits::min())
         << '\n';
  
    cout << "short: "
         << numeric_limits::min() << '\n';
  
    cout << "int: " << std::numeric_limits::min()
         << '\n';
    cout << "unsigned int: "
         << numeric_limits::min()
         << '\n';
  
    cout << "long long: "
         << numeric_limits::min()
         << '\n';
    cout << "float: "
         << numeric_limits::min()
         << '\n';
  
    cout << "double: "
         << numeric_limits::min()
         << '\n';
  
    cout << "size_t: "
         << numeric_limits::min()
         << '\n';
}
输出:
bool: 0
char: -128
unsigned char: 0
short: -32768
int: -2147483648
unsigned int: 0
long long: -9223372036854775808
float: 1.17549e-38
double: 2.22507e-308
size_t: 0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”