数据类型
C++ 支持各种各样的数据类型。在 C++ 中定义变量时,编译器根据变量声明的数据类型为该变量分配限定量的内存。每种数据类型需要不同量的内存。程序员可以根据应用程序的需要选择适当的数据类型。然而,每种数据类型的存储表示和机器指令在不同的机器上可能不同,尽管 C++ 指令在所有机器上都是相同的。
基础类型
- 无类型
void
: void 类型为无类型,与上面几种类型不同的是,不能将一个变量声明为 void 类型。但是函数的返回值允许为 void 类型,表示该函数无返回 值。 - 字符类型
char
- 布尔类型
bool
- 整数类型
int
- 浮点类型
float
,double
- 空指针类型(C++11)
std::nullptr_t
整数类型
由于历史原因,C++ 中布尔类型和字符类型会被视作特殊的整型。 在几乎所有的情况下都 不应该 将除 signed char 和 unsigned char 之外的字符类型作为整型使用。
整数类型一般按位宽有 5 个梯度:char,short,int,long,long long.
由于历史原因,整数类型的位宽有多种流行模型.
C++ 标准保证 1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
在 C++ 标准中,规定 int 的位数 至少 为 16 位。
事实上在现在的绝大多数平台,int 的位数均为 32 位。
对于 int 关键字,可以使用如下修饰关键字进行修饰:
符号性:
signed:表示带符号整数(默认); unsigned:表示无符号整数。 大小:
short:表示 至少 16 位整数; long:表示 至少 32 位整数; (C++11 起)long long:表示 至少 64 位整数。
在不引发歧义的情况下,允许省略部分修饰关键字,或调整修饰关键字的顺序。这意味着同一类型会存在多种等价表述。
例如 int,signed,int signed,signed int 表示同一类型,而 unsigned long 和 unsigned long int 表示同一类型。
c++中^
不是幂运算.幂运算是power(x,y)
;
string s = "Hello, world";
isdigit(s[2]);
isalpha(s[2]);
cout<<islower('a'); //非零
cout<<islower('2'); //输出0
cout<<islower('A'); //输出0
cout<<isupper('a'); //返回0
cout<<isupper('2'); //返回0
cout<<isupper('A'); //返回非零
tolower(s[0]);
toupper(s[1]);
isalnum(s[2]);