C++ 数据类型
使用任何语言编写程序时,您都需要使用各种变量来存储各种信息。变量只不过是用于存储值的保留内存位置。这意味着当您创建变量时,您会在内存中预留一些空间。
您可能希望存储各种数据类型的信息,例如字符、宽字符、整数、浮点数、双精度浮点数、布尔值等。操作系统会根据变量的数据类型分配内存,并决定哪些数据可以存储在保留内存中。
原始内置类型
C++ 为程序员提供了丰富的内置数据类型以及用户定义的数据类型。下表列出了七种基本的 C++ 数据类型 -
可以使用一个或多个以下类型修饰符来修改几种基本类型 -
- 有符号
- 无符号
- 短整型
- 长整型
下表显示了变量类型、存储值所需的内存大小以及此类变量可存储的最大值和最小值。
类型 | 典型位宽 | 典型范围 |
---|---|---|
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 8bytes | -9223372036854775808 to 9223372036854775807 |
signed long int | 8bytes | same as long int |
unsigned long int | 8bytes | 0 to 18446744073709551615 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | |
double | 8bytes | |
long double | 12bytes | |
wchar_t | 2 or 4 bytes | 1 wide character |
变量的大小可能与上表所示不同,具体取决于您使用的编译器和计算机。
示例
以下示例将在您的计算机上生成各种数据类型的正确大小。
#include <iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
本例使用 endl,它在每行后插入一个换行符,并使用 << 运算符将多个值传递到屏幕。我们还使用 sizeof() 运算符来获取各种数据类型的大小。
编译并执行上述代码后,将产生以下结果,该结果可能因机器而异 -
Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4
示例
以下是另一个示例:
#include <iostream> #include <limits> using namespace std; int main() { std::cout << "Int Min " << std::numeric_limits<int>::min() << endl; std::cout << "Int Max " << std::numeric_limits<int>::max() << endl; std::cout << "Unsigned Int Min " << std::numeric_limits<unsigned int>::min() << endl; std::cout << "Unsigned Int Max " << std::numeric_limits<unsigned int>::max() << endl; std::cout << "Long Int Min " << std::numeric_limits<long int>::min() << endl; std::cout << "Long Int Max " << std::numeric_limits<long int>::max() << endl; std::cout << "Unsigned Long Int Min " << std::numeric_limits<unsigned long int>::min() <<endl; std::cout << "Unsigned Long Int Max " << std::numeric_limits<unsigned long int>::max() << endl; }
派生数据类型
在 C++ 中,从预定义数据类型派生而来的数据类型称为派生数据类型。这些数据类型可分为四类,即:
1. 函数
函数是用户定义数据类型最简单的形式。它包含返回类型、函数名称和输入参数。
语法
return_type function_name(input_param1, input_param2){ <function_body> }
示例
#include <iostream> using namespace std; string func(int n){ //返回 n 是奇数还是偶数 if(n%2) return "Given number is Odd !"; else return "Given number is Even !"; } int main(){ int a; //输入一个数字 cin>>a; cout<<func(a); //一个简单的函数,用于检查 //数字是奇数还是偶数 return 0; }
输出
Given number is Even !
2. 数组
数组由一系列相同数据类型的元素组成。数组元素存储在存储空间中连续的内存位置。
语法
data_type array_name[array_size];
示例
#include <iostream> using namespace std; int main(){ int arr[5]={1,2,3,2,1}; //定义一个大小为 5 的整数数组 for(auto it:arr) cout<<it<<" "; //打印数组元素 return 0; }
输出
1 2 3 2 1
3. 指针
指针是对先前定义元素的引用。指针的值返回与其关联元素的地址位置。
语法
data_type * pointer_name=& variable_name;
示例
#include <iostream> using namespace std; int main() { int a=20; //声明变量a int *p= &a; //将指针赋值给a cout<<"Address of variable a: "<<p<<endl; cout<<"Value of variable a: "<<*p<<endl; return 0; }
输出
Address of variable a: 0x7ffc49a8637c Value of variable a: 20
4. 引用
引用变量用于创建具有相同引用的变量副本。因此,对引用变量的更改也会反映在原始变量上。
语法
data_type & reference_name= variable_name;
示例
#include <iostream> using namespace std; int main(){ int c=11; int& refer=c; cout<<"Initially value of integer is: "<<c<<endl; refer=121; cout<<"After changing value using refer variable :"<<c<<endl; return 0; }
输出
Initially value of integer is: 11 After changing value using refer variable :121
用户定义数据类型
用户自定义数据类型是指用户直观定义,无需使用任何预定义数据类型的数据类型。这些数据类型可以进一步分为五种类型,即:
1. 类
类是面向对象编程中定义的一种自定义数据类型,用于构造对象。它是对象的框架,可以包含构造函数、方法以及多态、继承等面向对象编程 (OOP) 概念。
语法
class Class_name{ <class body> class_name(parameters) { <constructor body> } return_type method_name(paremeters){ <method body> } }
示例
#include <iostream> using namespace std; class TP{ public: string tp; void print(){ cout<<tp<<endl; } }; int main(){ TP object; object.tp="I Love Tutorialspoint !!!"; object.print(); return 0; }
输出
I Love Tutorialspoint !!!
2. 结构体 (struct)
在结构体数据类型中,用户可以在结构体主体内引入多个原始数据类型。
语法
struct struct_name{ data_type1 var_name1; data_type2 var_name2; }
示例
#include <iostream> using namespace std; struct TP{ string tp; int grade; }; int main(){ TP object; object.tp="I Love Tutorialspoint !!!"; object.grade=10; cout<<object.tp<<endl; cout<<"How much would you rate it?"<<" : "<< object.grade; return 0; }
输出
I Love Tutorialspoint !!! How much would you rate it? : 10
3. 联合
联合类似于结构体。在这种情况下,所有变量的内存位置相同,并且所有变量共享同一个引用。因此,一个值的更改会导致所有其他值也随之更改。
语法
union union_name{ data_type var_name1; data_type var_name2; };
示例
#include <iostream> using namespace std; union TP{ int tp1,tp2; }; int main(){ union TP t; t.tp1=2; cout<<"Value of tp1 initially: "<<t.tp1<<endl; t.tp2=4; cout<<"When we change tp2, value of tp1 is : "<<t.tp1<<endl; return 0; }
输出
Value of tp1 initially: 2 When we change tp2, value of tp1 is : 4
4. 枚举(Enum)
枚举(或简称 enum)是一种用户定义的数据类型,用于在程序中为整型常量命名。这可以提高程序的可读性。
语法
enum enum_name{ var_name1 , var_name2, }
示例
#include <iostream> using namespace std; enum TP{ C, Java, Python, Ruby, Kotlin, Javascript, TypeScript, Others}; int main(){ enum TP course; cout<<"Which course do you love the most?"<<endl; course=Kotlin; cout<<"I love the "<<course+1<<"th course !!!"; return 0; }
输出
Which course do you love the most? I love the 5th course !!!
typedef 声明
您可以使用 typedef 为现有类型创建新名称。以下是使用 typedef 定义新类型的简单语法 -
typedef type newname;
例如,下面的代码告诉编译器 feet 是 int 的另一个名称 -
typedef int feet;
现在,以下声明完全合法,并创建了一个名为 distance 的整型变量 -
feet distance;
枚举类型
枚举类型声明一个可选的类型名称以及一组零个或多个可用作该类型值的标识符。每个枚举器都是一个常量,其类型为枚举。
创建枚举需要使用关键字 enum。枚举类型的一般形式为 -
enum enum-name { list of names } var-list;
其中,enum-name 是枚举的类型名称。名称列表以逗号分隔。
例如,以下代码定义了一个名为 colors 的颜色枚举,以及一个 color 类型的变量 c。最后,c 被赋值为"blue"。
enum color { red, green, blue } c; c = blue;
默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,依此类推。但您可以通过添加初始化器来赋予名称特定的值。例如,在下面的枚举中,green 的值为 5。
enum color { red, green = 5, blue };
这里,blue 的值为 6,因为每个名称都比它前面的名称大 1。