C++ 常忽視的要點

1、在main()函數中,結尾不含返回值,默認爲return 0

2、C++中通常能使用回車的地方都能夠使用空格,反之同樣

3、C++ 與C的不同之一, C++中不一定每個變量都要在函數的開始位置聲明,只要保證在第一次使用之前進行聲明就可以,然而在C語言中 必須先全部聲明然後才能開始使用。

4、4種使用名空間的方法,
1. 在函數體外面使用, 全體函數都可以使用名空間的內容
2. 在函數內部使用,當前函數使用有效
3. 使用如 using std:cout 這樣的方法 ,激活對應的空間函數
4. std:cout的使用方法

5、在C++的變量中,__XX 兩個下劃線開頭和 _A下劃線和大寫字母打頭的名稱保留給系統資源使用。_x保留給全局標識符。

6、in被設置爲計算機運行時候效率最高的長度,若是沒有其他原因,應該使用int

7、變量以零開頭 如 042應該視爲是8進制的數
8、在cout的輸出前修改格式


cout<< hex <<19 ;// 將會以16進制輸出
cout<< oct <<19 ; //以8進制的形式輸出

9、數字後面的U u L 指定存儲方式

10、

變量的類型轉換 等式 A=B+C

  1. B C 變量中要是有一個是浮點型,則將另外一個也轉成浮點型
cout << "(long double)1+ (float)1) :" << typeid((long double)1 + (float)1).name() << endl;
cout << "(long double)1+ 1)        :"<<typeid((long double)1+1).name() << endl;
cout << "(double)1+ 1)             :" << typeid((double)1 + 1).name() << endl;
cout << "(float)1+ 1)              :" << typeid((float)1 + 1).name() << endl;

這裏寫圖片描述
2. bool 、char 、short、 unsigned char、 unsigned short 類型相加,先轉爲 int 類型再相加。無論B C 中是否有 int ,這種做法的目的是爲了獲得更好的計算速度。有比int 更高類型的直接轉爲更高類型。

cout << "(bool)1 + (short)1                    :" << typeid((bool)1 + (short)1).name() << endl;
cout << "(char)1 + (short)1                    :" << typeid((char)1 + (short)1).name() << endl;
cout << "(char)1 + (unsigned short)1           :" << typeid((char)1 + (unsigned short)1).name() << endl;
cout << "(unsigned char)1 + (short)1           :" << typeid((unsigned char)1 + (short)1).name() << endl;
cout << "(unsigned char)1 + (unsigned short)1  :" << typeid((unsigned char)1 + (unsigned short)1).name() << endl;
cout << "(long)1 + (short)1                    :" << typeid((long)1 + (short)1).name() << endl;

image
3. 若 BC 爲有符號和無符號的加減 ,有符號的取值能包含所有無符號的值,則轉爲有符號數的類型。若不能不包含,則轉高級類型的無符號表示

cout << "(unsigned short)1+(int)1            :" << typeid((unsigned short)1 + (int)1).name() << endl;
cout << "(int)1 + (long int)1                :" << typeid((int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1 + (long int)1       :" << typeid((unsigned int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1 + (long long int)1  :" << typeid((unsigned int)1 + (long long int)1).name() << endl;
cout << "(int)1 + (unsigned long int)1       :" << typeid((int)1 + (unsigned long int)1).name() << endl;

cout << "sizeof(int)           :"<< sizeof(int) << endl;
cout << "sizeof(long int)      :"<< sizeof(long int) << endl;
cout << "sizeof(long long int) :"<<sizeof(long long int) << endl;

image

其中第三個 之所以沒有轉成 long int 類型 ,從後面 的long int 和int 的長度可以看出兩個的長度是一樣的。
4. 若兩個數都是有符號或者無符號,則轉爲級別更高的類型

cout << "(short)1+(int)1                      :" << typeid((short)1 + (int)1).name() << endl;
cout << "(short)1+(long )1                    :" << typeid((short)1 + (long int)1).name() << endl;
cout << "(unsigned short)1+(unsigned int)1    :" << typeid((unsigned short)1 + (unsigned int)1).name() << endl;
cout << "(unsigned short)1+(unsigned long )1  :" << typeid((unsigned short)1 + (unsigned long int)1).name() << endl;

cout << "(int)1+(long int)1                   :" << typeid((int)1 + (long int)1).name() << endl;
cout << "(unsigned int)1+(unsigned long )1    :" << typeid((unsigned int)1 + (unsigned long int)1).name() << endl;

image

11、強制類型轉換的兩種形式。

int (number)   //純粹的C++
int() number   //來自C 語言
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章