This is a piece of C++ code. After running, it will output information such as the number of bytes occupied by different data types under the current platform, the maximum value and the minimum value. The specific explanation is as follows:
First, include two header files:
iostream
: Standard header file for input and output streams, used for standard input and output.std
Containscout
and defined in the namespaceendl
.limits
: The header file of numerical extreme values, which defines the value range of many data types.
Then, output the different types of size, maximum value and minimum value respectively in main()
the function . in:
cout <<
: indicates output.<<
In C++ is a left shift operator used to insert data into the output stream."type: \t\t"
: This is a string constant representing the text content of the output. Among them\t
, the escape character represents a tab character,\
which is the escape character of the escape character, so two can be used\
to output one\
.
So what this line of code does is output the sum"type: \t\t"
of the strings"************size**************"
to the console, adding a newline at the end of the line.(numeric_limits<T>::max)()
: is a template function that gets the maximum value of the specified type. Among them,T
is the specified type,::max
is the static member function of this type, and returns the maximum value that this type can store.(numeric_limits<T>::min)()
Similarly, get the minimum value.endl
: console newline character, indicating the end of an output and a newline.
Finally , the data types output by the code include //////////////, etc. , as well as the number of bytes they occupy and the maximum and minimum bool
values .char
signed char
unsigned char
wchar_t
short
int
unsigned
long
unsigned long
double
long double
float
size_t
string
#include<iostream>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************" << endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************" << endl;
return 0;
}
Let me add:
-
sizeof(T)
: is an operator used to view the number of bytes occupiedT
by . -
std::numeric_limits
: This is a template class that contains many restrictions on numeric types. Through the static member functions it provides, information such as maximum and minimum values of different types can be obtained. -
(numeric_limits<T>::max)()
and(numeric_limits<T>::min)()
Both use a pair of extra parentheses()
, their function is to call the return value of the static member function, because these functions return an object, you need to call it to get the specific return value. -
::
The first partnumeric_limits<T>
isT
a description of the type, specifying its type; the lattermax()
andmin()
are static function names. -
using namespace std;
It uses the namespace mechanism of C++, which means using all the symbols defined in thestd
standard namespace.
In short, the function of this code is to output the data size and value range of different types under the current platform. It is a commonly used small tool for learning C++ and understanding platform characteristics and data types.