使用limits頭文件,計算某種數據類型的最大最小值

寫算法的時候可能會用到的一個小工具吧,使用如下:

/*

在C++中,求一種數據類型的最大最小值

使用 <limits> 頭文件

*/

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<limits>
using namespace std;

int main()
{
	//  unsigned short
	cout << "unsigned short:" << endl;
	cout << "      min = " << numeric_limits<unsigned short>::min() << endl;
	cout << "      max = " << numeric_limits<unsigned short>::max() << endl;

	// short
	cout << "short:" << endl;
	cout << "      min = " << numeric_limits<short>::min() << endl;
	cout << "      max = " << numeric_limits<short>::max() << endl;

	// int
	cout << "int:" << endl;
	cout << "      min = " << numeric_limits<int>::min() << endl;
	cout << "      max = " << numeric_limits<int>::max() << endl;

	// long
	cout << "long:" << endl;
	cout << "      min = " << numeric_limits<long>::min() << endl;
	cout << "      max = " << numeric_limits<long>::max() << endl;

	// long long
	cout << "long long:" << endl;
	cout << "      min = " << numeric_limits<long long>::min() << endl;
	cout << "      max = " << numeric_limits<long long>::max() << endl;

	
	system("pause");
	return EXIT_SUCCESS;
}

函數輸出如下:

unsigned short:
      min = 0
      max = 65535
short:
      min = -32768
      max = 32767
int:
      min = -2147483648
      max = 2147483647
long:
      min = -2147483648
      max = 2147483647
long long:
      min = -9223372036854775808
      max = 9223372036854775807

希望大家需要的時候,能方便用到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章