菜鳥教程學習筆記:
三字符組
三字符組就是用於表示另一個字符的三個字符序列,又稱爲三字符序列。三字符序列總是以兩個問號開頭。
三字符序列不太常見,但 C++ 標準允許把某些字符指定爲三字符序列。以前爲了表示鍵盤上沒有的字符,這是必不可少的一種方法。
三字符序列可以出現在任何地方,包括字符串、字符序列、註釋和預處理指令。
三字符組 | 替換 |
---|---|
??= | # |
??/ | \ |
??' | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
基本數據類型:
類型 | 關鍵字 |
---|---|
布爾型 | bool |
字符型 | char |
整型 | int |
浮點型 | float |
雙浮點型 | double |
無類型 | void |
寬字符型 | wchar_t |
一些基本類型可以使用一個或多個類型修飾符進行修飾:
- signed
- unsigned
- short
- long
下表顯示了各種變量類型在內存中存儲值時需要佔用的內存,以及該類型的變量所能存儲的最大值和最小值。
注意:不同系統會有所差異。
類型 | 位 | 範圍 |
---|---|---|
char | 1 個字節 | -128 到 127 或者 0 到 255 |
unsigned char | 1 個字節 | 0 到 255 |
signed char | 1 個字節 | -128 到 127 |
int | 4 個字節 | -2147483648 到 2147483647 |
unsigned int | 4 個字節 | 0 到 4294967295 |
signed int | 4 個字節 | -2147483648 到 2147483647 |
short int | 2 個字節 | -32768 到 32767 |
unsigned short int | 2 個字節 | 0 到 65,535 |
signed short int | 2 個字節 | -32768 到 32767 |
long int | 8 個字節 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
signed long int | 8 個字節 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
unsigned long int | 8 個字節 | 0 to 18,446,744,073,709,551,615 |
float | 4 個字節 | +/- 3.4e +/- 38 (~7 個數字) |
double | 8 個字節 | +/- 1.7e +/- 308 (~15 個數字) |
long double | 16 個字節 | +/- 1.7e +/- 308 (~15 個數字) |
wchar_t | 2 或 4 個字節 | 1 個寬字符 |
#include<iostream> #include<string> #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; }
枚舉:
創建枚舉,需要使用關鍵字 enum。枚舉類型的一般形式爲:
enum 枚舉名{ 標識符[=整型常數], 標識符[=整型常數], ... 標識符[=整型常數] } 枚舉變量;
如果枚舉沒有初始化, 即省掉"=整型常數"時, 則從第一個標識符開始。
例如,下面的代碼定義了一個顏色枚舉,變量 c 的類型爲 color。最後,c 被賦值爲 "blue"。
enum color { red, green, blue } c; c = blue;
默認情況下,第一個名稱的值爲 0,第二個名稱的值爲 1,第三個名稱的值爲 2,以此類推。但是,您也可以給名稱賦予一個特殊的值,只需要添加一個初始值即可。例如,在下面的枚舉中,green 的值爲 5。
enum color { red, green=5, blue };
在這裏,blue 的值爲 6,因爲默認情況下,每個名稱都會比它前面一個名稱大 1,但 red 的值依然爲 0。