一个常用的 C++ 学习、了解平台特性和数据类型的小工具(源代码)

这是一段C++代码,运行后会输出不同数据类型在当前平台下所占字节数、最大值和最小值等信息。具体解释如下:

首先,包含两个头文件:

  • iostream:输入输出流的标准头文件,用于标准输入输出。包含定义在 std 命名空间的 coutendl
  • limits:数值极值的头文件,定义了许多数据类型的取值范围。

然后,在 main() 函数中分别输出不同类型的大小、最大值和最小值。其中:

  • cout << :表示输出。 << 在 C++ 中是一个左移运算符,用于向输出流中插入数据。
  • "type: \t\t": 这是一个字符串常量,表示输出的文本内容。其中\t是转义字符代表一个制表符,\是转义字符的转义符,所以要用两个\才能输出一个\
    因此,这行代码的作用是将字符串"type: \t\t""************size**************"输出到控制台,并在行末添加一个换行符。
  • (numeric_limits<T>::max)(): 是一个模板函数,获取指定类型的最大值。其中,T 是指定类型,::max 是这个类型的静态成员函数,返回这个类型所能存放的最大值。(numeric_limits<T>::min)() 同理,获取最小值。
  • endl: 控制台换行符,表示一个输出结束并换行。

最后,代码输出的数据类型包括 bool/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;
}

补充说明一下:

  • sizeof(T):是一个运算符,用于查看指定类型 T 所占用的字节数。

  • std::numeric_limits:这是一个模板类,包含了很多数值类型的限制。通过它提供的静态成员函数可以获取不同类型的最大最小值等信息。

  • (numeric_limits<T>::max)()(numeric_limits<T>::min)() 都使用了一对额外的括号 (),它们的作用是调用静态成员函数的返回值,因为这些函数返回的是一个对象,需要对其进行调用才能获得具体的返回值。

  • :: 前面的部分 numeric_limits<T> 是对 T 类型的描述,指定其所属类型;后面的 max()min() 是静态函数名。

  • using namespace std; 则是使用了 C++ 的命名空间机制,表示使用 std 标准命名空间中定义的所有符号。

总之,这段代码的作用是输出不同类型在当前平台下的数据大小和取值范围,是一个常用的 C++ 学习、了解平台特性和数据类型的小工具。

猜你喜欢

转载自blog.csdn.net/weixin_51624736/article/details/130962845
今日推荐