《C++ Primer Plus》第三章 处理数据 ——Part 1

mac2022-06-30  32

重要知识点

内置的C++类型分为两组,基本类型和复合类型。基本类型包括整数和浮点数。变量的命名 不要以一个或者两个下划线开头,或者一个下划线加大写字母开头。变量命名不能以数字开头,不能使用连字符-圆点.和空格 。变量名有意义。 符合变量命名规则的示例:int counterForSDU; int counter_for_SDU;。

整型

类型占用位最小值到最大值short2-32768 - 32767int4-2147483648 - 2147483647long一般4-2147483648 - 2147483647long long8-9223372036854775808 - 9223372036854775807 用unsigned前缀声明无符号类型

计算机内存的基本单元是位(bit)。字节(byte)通常指的是8位的内存单元,字节是描述计算机内存量的度量单位。

sizeof运算符和<climits>函数

注意sizeof并不是一个函数。

// limits.cpp -- some integer limits #include <iostream> #include <climits> // use limits.h for older systems int main() { using namespace std; int n_int = INT_MAX; // initialize n_int to max int value short n_short = SHRT_MAX; // symbols defined in climits file long n_long = LONG_MAX; long long n_llong = LLONG_MAX; // sizeof operator yields size of type or of variable cout << "int is " << sizeof (int) << " bytes." << endl; //对于类型名用小括号括起来 cout << "short is " << sizeof n_short << " bytes." << endl; cout << "long is " << sizeof n_long << " bytes." << endl; cout << "long long is " << sizeof n_llong << " bytes." << endl; cout << endl; cout << "Maximum values:" << endl; cout << "int: " << n_int << endl; cout << "short: " << n_short << endl; cout << "long: " << n_long << endl; cout << "long long: " << n_llong << endl << endl; cout << "Minimum int value = " << INT_MIN << endl; cout << "Bits per byte = " << CHAR_BIT << endl; return 0; }

输出结果:

int is 4 bytes. short is 2 bytes. long is 4 bytes. long long is 8 bytes. Maximum values: int: 2147483647 short: 32767 long: 2147483647 long long: 9223372036854775807 Minimum int value = -2147483648 Bits per byte = 8 climits库中的符号常量 常量名称含义CHAR_BITchar 的位数CHAR_MAXchar 的最大值CHAR_MINchar 的最小值SCHAR_MAXsigned char 的最大值SCHAR_MINsigned char 的最小值UCHAR_MAXunsigned char 的最大值SHRT_MAXshort 的最大值SHRT_MINshort 的最小值USHRT_MAXunsigned short 的最大值INT_MAXint 的最大值INT_MINint 的最小值UNIT_MAXunsigned int 的最大值LONG_MAXlong 的最大值LONG_MINlong 的最小值LONG_MAXunsigned long 的最大值LLONG_MAXlong long的最大值LLONG_MINlong long的最小值ULLONG_MAXunsigned long long 的最大值

溢出

// exceed.cpp -- exceeding some integer limits #include <iostream> #define ZERO 0 // makes ZERO symbol for 0 value #include <climits> // defines INT_MAX as largest int value int main() { using namespace std; short sam = SHRT_MAX; // initialize a variable to max value unsigned short sue = sam;// okay if variable sam already defined cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl << "Add $1 to each account." << endl << "Now "; sam = sam + 1; sue = sue + 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\nPoor Sam!" << endl; sam = ZERO; sue = ZERO; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl; cout << "Take $1 from each account." << endl << "Now "; sam = sam - 1; sue = sue - 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl << "Lucky Sue!" << endl; return 0; }

运行结果:

Sam has 32767 dollars and Sue has 32767 dollars deposited. Add $1 to each account. Now Sam has -32768 dollars and Sue has 32768 dollars deposited. Poor Sam! Sam has 0 dollars and Sue has 0 dollars deposited. Take $1 from each account. Now Sam has -1 dollars and Sue has 65535 dollars deposited. Lucky Sue!

整型字面值

C++使用前一(两)位来标识数字常量的基数。 如果第一位是1到9,则为十进制。如果第一位是0,第二位是1到7,则为八进制。如果前两位是0x,则为十六进制。

进制转化

进制之间的转化

进制表示

进制表示十进制dec十六进制hex八进制oct 修改cout输出值的进制. // hexoct2.cpp -- display values in hex and octal #include <iostream> using namespace std; int main() { using namespace std; int chest = 42; int waist = 42; int inseam = 42; cout << "Monsieur cuts a striking figure!" << endl; cout << "chest = " << chest << " (decimal for 42)" << endl; cout << hex; // manipulator for changing number base cout << "waist = " << waist << " (hexadecimal for 42)" << endl; cout << oct; // manipulator for changing number base cout << "inseam = " << inseam << " (octal for 42)" << endl; return 0; }

输出结果:

Monsieur cuts a striking figure! chest = 42 (decimal for 42) waist = 2a (hexadecimal for 42) inseam = 52 (octal for 42) 注意hex、oct、dec标记的效力是使cout对未来遭遇的所有整型变量都使用指定的方式输出(直到被下一个标记改变位置),而不仅限于标记之后的一个变量.

char类型

char类型是专门存储字符而设计的。char ch = 'N';字符用单引号括起来,字符串用双引号括起来。char类型占用一个字节,char也可以表示比short类型更小的整数。代码 char ch; cin >> ch;,如果输入数字如5,上述代码将把5视为一个字符并转化为相应的ASCII码存储。 #include <iostream> using namespace std; int main() { char ch; cout << "Put in a char: "; cin >> ch; cout << "The ASCII code for " << ch << " is " << int(ch) << endl; return 0; }

输出结果:

Put in a char: 9 The ASCII code for 9 is 57

cout.put()函数

cout.put()可以将括号中的数字转化为字符输出。

#include <iostream> using namespace std; int main() { int n = 112; cout.put(n); cout << endl; // cout.put(103); return 0; }

输出结果:

p g

char字面值

对于常规字符,将字符用单引号括起来。对于某些无法输入或者引起歧义的字符,使用转义序列。如\n \a \b \t // bondini.cpp -- using escape sequences #include <iostream> int main() { using namespace std; cout << "\aOperation \"HyperHype\" is now activated!\n"; cout << "Enter your agent code:________\b\b\b\b\b\b\b\b"; long code; cin >> code; cout << "\aYou entered " << code << "...\n"; cout << "\aCode verified! Proceed with Plan Z3!\n"; // cin.get(); // cin.get(); return 0; }

输出结果:

Operation "HyperHype" is now activated! Enter your agent code:12345678 You entered 12345678... Code verified! Proceed with Plan Z3! 可以基于字符的八进制和十六进制编码使用转义序列。 #include <iostream> using namespace std; int main() { char ch = '\106'; cout << ch << endl; ch = '\046'; cout << ch << endl; ch = '\x74';//不能写成'\0x74' cout << ch << endl; return 0; }

输出结果:

F & t

bool类型

bool值由true和false组成。也可以用数字来表示bool的值,此时,0表示false,非0数表示true。 #include <iostream> using namespace std; int main() { bool sym; sym = 1; cout << sym << endl; sym = 3; cout << sym << endl; sym = 1.3; cout << sym << endl; sym = -1; cout << sym << endl; sym = -1.3; cout << sym << endl; sym = 0; cout << sym << endl; return 0; }

输出结果:

1 1 1 1 1 0

const限定符

const int ans = 3;

使用限定符后,ans是一个不可更改的常量。

最新回复(0)