C++入門到精通【第005講】----C++ 數(shù)據(jù)類型
使用編程語言進行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個變量時,就會在內(nèi)存中保留一些空間。
您可能需要存儲各種數(shù)據(jù)類型(比如字符型、寬字符型、整型、浮點型、雙浮點型、布爾型等)的信息,操作系統(tǒng)會根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲什么。
基本的內(nèi)置類型
C++ 為程序員提供了種類豐富的內(nèi)置數(shù)據(jù)類型和用戶自定義的數(shù)據(jù)類型。下表列出了七種基本的 C++ 數(shù)據(jù)類型:

typedef short int wchar_t;
所以 wchar_t 實際上的空間是和 short int 一樣。
一些基本類型可以使用一個或多個類型修飾符進行修飾:
signed
unsigned
short
long
下表顯示了各種變量類型在內(nèi)存中存儲值時需要占用的內(nèi)存,以及該類型的變量所能存儲的最大值和最小值。
注意:不同系統(tǒng)會有所差異,一字節(jié)為 8 位。
注意:默認情況下,int、short、long都是帶符號的,即 signed。
注意:long int 8 個字節(jié),int 都是 4 個字節(jié),早期的 C 編譯器定義了 long int 占用 4 個字節(jié),int 占用 2 個字節(jié),新版的 C/C++ 標準兼容了早期的這一設(shè)定。


從上表可得知,變量的大小會根據(jù)編譯器和所使用的電腦而有所不同。
下面實例會輸出您電腦上各種數(shù)據(jù)類型的大小。
實例
#include<iostream>??
#include <limits>
?
using namespace std;??
??
int main()??
{??
? ? cout << "type: \t\t" << "************size**************"<< endl;??
? ? cout << "bool: \t\t" << "所占字節(jié)數(shù):" << sizeof(bool);??
? ? cout << "\t最大值:" << (numeric_limits<bool>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;??
? ? cout << "char: \t\t" << "所占字節(jié)數(shù):" << sizeof(char);??
? ? cout << "\t最大值:" << (numeric_limits<char>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;??
? ? cout << "signed char: \t" << "所占字節(jié)數(shù):" << sizeof(signed char);??
? ? cout << "\t最大值:" << (numeric_limits<signed char>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;??
? ? cout << "unsigned char: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned char);??
? ? cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;??
? ? cout << "wchar_t: \t" << "所占字節(jié)數(shù):" << sizeof(wchar_t);??
? ? cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;??
? ? cout << "short: \t\t" << "所占字節(jié)數(shù):" << sizeof(short);??
? ? cout << "\t最大值:" << (numeric_limits<short>::max)();??
? ? cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;??
? ? cout << "int: \t\t" << "所占字節(jié)數(shù):" << sizeof(int);??
? ? cout << "\t最大值:" << (numeric_limits<int>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;??
? ? cout << "unsigned: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned);??
? ? cout << "\t最大值:" << (numeric_limits<unsigned>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;??
? ? cout << "long: \t\t" << "所占字節(jié)數(shù):" << sizeof(long);??
? ? cout << "\t最大值:" << (numeric_limits<long>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;??
? ? cout << "unsigned long: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned long);??
? ? cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;??
? ? cout << "double: \t" << "所占字節(jié)數(shù):" << sizeof(double);??
? ? cout << "\t最大值:" << (numeric_limits<double>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;??
? ? cout << "long double: \t" << "所占字節(jié)數(shù):" << sizeof(long double);??
? ? cout << "\t最大值:" << (numeric_limits<long double>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;??
? ? cout << "float: \t\t" << "所占字節(jié)數(shù):" << sizeof(float);??
? ? cout << "\t最大值:" << (numeric_limits<float>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;??
? ? cout << "size_t: \t" << "所占字節(jié)數(shù):" << sizeof(size_t);??
? ? cout << "\t最大值:" << (numeric_limits<size_t>::max)();??
? ? cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;??
? ? cout << "string: \t" << "所占字節(jié)數(shù):" << sizeof(string) << endl;??
? ? // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;??
? ? cout << "type: \t\t" << "************size**************"<< endl;??
? ? return 0;??
}
本實例使用了 endl,這將在每一行后插入一個換行符,<< 運算符用于向屏幕傳多個值,sizeof() 運算符用來獲取各種數(shù)據(jù)類型的大小。
當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生以下的結(jié)果,結(jié)果會根據(jù)所使用的計算機而有所不同:
type:? ? ? ? ?************size**************
bool:? ? ? ? ?所占字節(jié)數(shù):1? ? 最大值:1? ? ? ? 最小值:0
char:? ? ? ? ?所占字節(jié)數(shù):1? ? 最大值:? ? ? ? 最小值:?
signed char:? ? ?所占字節(jié)數(shù):1? ? 最大值:? ? ? ? 最小值:?
unsigned char:? ? ?所占字節(jié)數(shù):1? ? 最大值:?? ? ? ? 最小值:
wchar_t:? ? ?所占字節(jié)數(shù):4? ? 最大值:2147483647? ? ? ? 最小值:-2147483648
short:? ? ? ? ?所占字節(jié)數(shù):2? ? 最大值:32767? ? ? ? 最小值:-32768
int:? ? ? ? ?所占字節(jié)數(shù):4? ? 最大值:2147483647? ? 最小值:-2147483648
unsigned:? ? ?所占字節(jié)數(shù):4? ? 最大值:4294967295? ? 最小值:0
long:? ? ? ? ?所占字節(jié)數(shù):8? ? 最大值:9223372036854775807? ? 最小值:-9223372036854775808
unsigned long:? ? ?所占字節(jié)數(shù):8? ? 最大值:18446744073709551615? ? 最小值:0
double:? ? ?所占字節(jié)數(shù):8? ? 最大值:1.79769e+308? ? 最小值:2.22507e-308
long double:? ? ?所占字節(jié)數(shù):16? ? 最大值:1.18973e+4932? ? 最小值:3.3621e-4932
float:? ? ? ? ?所占字節(jié)數(shù):4? ? 最大值:3.40282e+38? ? 最小值:1.17549e-38
size_t:? ? ?所占字節(jié)數(shù):8? ? 最大值:18446744073709551615? ? 最小值:0
string:? ? ?所占字節(jié)數(shù):24
type:? ? ? ? ?************size**************
typedef 聲明
您可以使用?typedef?為一個已有的類型取一個新的名字。下面是使用 typedef 定義一個新類型的語法:
typedef type newname;
例如,下面的語句會告訴編譯器,feet 是 int 的另一個名稱:
typedef int feet;
現(xiàn)在,下面的聲明是完全合法的,它創(chuàng)建了一個整型變量 distance:
feet distance;
枚舉類型
枚舉類型(enumeration)是C++中的一種派生數(shù)據(jù)類型,它是由用戶定義的若干枚舉常量的集合。
如果一個變量只有幾種可能的值,可以定義為枚舉(enumeration)類型。所謂"枚舉"是指將變量的值一一列舉出來,變量的值只能在列舉出來的值的范圍內(nèi)。
創(chuàng)建枚舉,需要使用關(guān)鍵字?enum。枚舉類型的一般形式為:
enum 枚舉名{?
? ? ?標識符[=整型常數(shù)],?
? ? ?標識符[=整型常數(shù)],?
...?
? ? 標識符[=整型常數(shù)]
} 枚舉變量;
如果枚舉沒有初始化, 即省掉"=整型常數(shù)"時, 則從第一個標識符開始。
例如,下面的代碼定義了一個顏色枚舉,變量 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。
類型轉(zhuǎn)換
類型轉(zhuǎn)換是將一個數(shù)據(jù)類型的值轉(zhuǎn)換為另一種數(shù)據(jù)類型的值。
C++ 中有四種類型轉(zhuǎn)換:靜態(tài)轉(zhuǎn)換、動態(tài)轉(zhuǎn)換、常量轉(zhuǎn)換和重新解釋轉(zhuǎn)換。
靜態(tài)轉(zhuǎn)換(Static Cast)
靜態(tài)轉(zhuǎn)換是將一種數(shù)據(jù)類型的值強制轉(zhuǎn)換為另一種數(shù)據(jù)類型的值。
靜態(tài)轉(zhuǎn)換通常用于比較類型相似的對象之間的轉(zhuǎn)換,例如將 int 類型轉(zhuǎn)換為 float 類型。
靜態(tài)轉(zhuǎn)換不進行任何運行時類型檢查,因此可能會導(dǎo)致運行時錯誤。
實例
int?i?=?10;
float?f?=?static_cast<float>(i);?// 靜態(tài)將int類型轉(zhuǎn)換為float類型
動態(tài)轉(zhuǎn)換(Dynamic Cast)
動態(tài)轉(zhuǎn)換通常用于將一個基類指針或引用轉(zhuǎn)換為派生類指針或引用。動態(tài)轉(zhuǎn)換在運行時進行類型檢查,如果不能進行轉(zhuǎn)換則返回空指針或引發(fā)異常。
實例
class?Base?{};
class Derived : public Base {};
Base* ptr_base = new Derived;
Derived* ptr_derived = dynamic_cast<Derived*>(ptr_base); // 將基類指針轉(zhuǎn)換為派生類指針
常量轉(zhuǎn)換(Const Cast)
常量轉(zhuǎn)換用于將 const 類型的對象轉(zhuǎn)換為非 const 類型的對象。
常量轉(zhuǎn)換只能用于轉(zhuǎn)換掉 const 屬性,不能改變對象的類型。
實例
const int i = 10;
int& r = const_cast<int&>(i); // 常量轉(zhuǎn)換,將const int轉(zhuǎn)換為int
重新解釋轉(zhuǎn)換(Reinterpret Cast)
重新解釋轉(zhuǎn)換將一個數(shù)據(jù)類型的值重新解釋為另一個數(shù)據(jù)類型的值,通常用于在不同的數(shù)據(jù)類型之間進行轉(zhuǎn)換。
重新解釋轉(zhuǎn)換不進行任何類型檢查,因此可能會導(dǎo)致未定義的行為。
實例
int?i?=?10;
float?f?=?reinterpret_cast<float&>(i);?// 重新解釋將int類型轉(zhuǎn)換為float類型