要求
- ranges.cpp Write a program that outputs the largest and smallest values of the following types:
char, wchar_t, short, int, long, float, double,
unsigned and long long.
编写一个程序,输出以下类型的最大值和最小值:
char,wchar,t,short,int,long,float,double,
unsigned 和long long。
知识点
可以利用库函数<limits>的模板函数
numeric_limits<T>::max()
numeric_limits<T>::min()
numeric_limits<T>::lowest()
需要注意的是 对于float和double类型,min输出的是最大负数(即能表示的最大精度负数)
所以这里得使用lowest
代码
#include<iostream>
#include<limits>
using namespace std;
int main()
{
cout << "max of char : " << (int)numeric_limits<char>::max() <<" min of char : "<<(int)numeric_limits<char>::min()<< endl;
cout << "max of wchar_t : " << numeric_limits<wchar_t>::max() <<" min of wchar_t : "<<numeric_limits<wchar_t>::min()<< endl;
cout << "max of short : " << numeric_limits<short>::max() <<" min of short : "<<numeric_limits<short>::min()<< endl;
cout << "max of int : " << numeric_limits<int>::max() <<" min of int : "<<numeric_limits<int>::min()<< endl;
cout << "max of long : " << numeric_limits<long>::max() <<" min of long : "<<numeric_limits<long>::min()<< endl;
cout << "max of float : " << numeric_limits<float>::max() <<" min of float : "<<numeric_limits<float>::lowest()<< endl;
cout << "max of double : " << numeric_limits<double>::max() <<" min of double : "<<numeric_limits<double>::lowest()<< endl;
//float和double的min代表的是能表示的最大负数,最小数是lowest
cout << "max of unsigned : " << numeric_limits<unsigned>::max() <<" min of unsigned : "<<numeric_limits<unsigned>::min()<< endl;
cout << "max of long long : " << numeric_limits<long long>::max() <<" min of long long : "<<numeric_limits<long long>::min()<< endl;
return 0;
}