前言:
各种变量类型在内存中存储都需要占用内存(即字节数),并且该类型都有各自能存储的最大值以及最小值,因电脑而异。
示例代码:
// type_value.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <limits>
using namespace std;
//by zhaocl
int main()
{
cout << "---------------type begin---------------------" << endl;
//bool
cout << "bool size:" << sizeof( bool );
cout << "\t MaxValue:" << ( numeric_limits<bool>::max )();
cout << "\t MinValue:" << ( numeric_limits<bool>::min )() << endl;
//int
cout << "int size:" << sizeof( int );
cout << "\t MaxValue:" << ( numeric_limits<int>::max )();
cout << "\t MinValue:" << ( numeric_limits<int>::min )() << endl;
//unsigned int
cout << "unsigned int size:" << sizeof( unsigned int );
cout << "\t MaxValue:" << ( numeric_limits<unsigned int>::max )();
cout << "\t MinValue:" << ( numeric_limits<unsigned int>::min )() << endl;
//the same as other ...
cout << "---------------type end---------------------" << endl;
system( "pause" );
return 0;
}
知识点:
(numeric_limits<type>::max)()的使用