前言:
C++專欄筆記來源于觀看視頻筆記
配套視頻:
https://www.bilibili.com/video/BV1et411b73Z?p=16
基本代碼框架:
#include <iostream>
using namespace std;
int main()
{system("pause");//等待程序return 0;
}
輸出HelloWorld:
#include <iostream>
using namespace std;
int main()
{cout <<"HelloWorld"<<endl;//""之間的是你要輸出的東西system("pause");//等待程序return 0;
}
變量:
作用:給一段指定的內存空間起名,方便操作這段內存。
語法:數據類型 ??變量名=初始值;
示例:
#include <iostream>
using namespace std;
int main()
{int a=10;cout <<"a="<<a<<endl;system("pause");//等待程序return 0;
}
常量:
作用:用于記錄程序中不可更改的數據
C++定義常量兩種方式:
1、#define宏常量:#define ?常量名? 常量值
- 通常在文件上方定義,表示一個常量
2、const修飾的變量 :const ?數據類型?常量名=常量值
- 通常在變量定義前加關鍵字const,修飾該變量為常量不可修改
示例:
#include <iostream>
using namespace std;
#define day 7 //宏常量
int main()
{//day = 8; //報錯,宏常量不可以修改cout <<"一周里共有"<<day<<"天"<<endl;//2、const修飾變量const int month = 12;cout << "一年里總共有 " << month << " 個月份" << endl;//month = 24; //報錯,常量是不可以修改的system("pause");//等待程序return 0;
}
關鍵字:
作用:關鍵字是C++中預先保留的單詞(標識符)
注意:在定義變量或者常量時候,不要用關鍵字
C++關鍵字如下:
提示:在給變量或者常量起名稱時候,不要用C++得關鍵字,否則會產生歧義。
標識符命名規則:
作用:C++規定給標識符(變量、常量)命名時,有一套自己的規則
- 標識符不能是關鍵字
- 標識符只能由字母、數字、下劃線組成,無空格
- 第一個字符必須為字母或下劃線
- 標識符中字母區分大小寫
建議:給標識符命名時,爭取做到見名知意的效果,方便自己和他人的閱讀
數據類型:
C++規定在創建一個變量或者常量時,必須要指定出相應的數據類型,否則無法給變量分配內存。
1、整型
作用:整型變量表示的是整數類型的數據
C++中能夠表示整型的類型有以下幾種方式,區別在于所占內存空間不同:
2、sizeof關鍵字
作用:利用sizeof關鍵字可以統計數據類型所占內存大小(輸出的是字節數)
語法: sizeof( 數據類型 / 變量)
示例:
#include <iostream>
using namespace std;
int main()
{cout << "short 類型所占內存空間為: " << sizeof(short) << endl;cout << "int 類型所占內存空間為: " << sizeof(int) << endl;cout << "long 類型所占內存空間為: " << sizeof(long) << endl;cout << "long long 類型所占內存空間為: " << sizeof(long long) << endl;system("pause");//等待程序return 0;
}
整型結論:short < int <= long <= long long
3、實型(浮點型)
作用:用于表示小數
浮點型變量分為兩種:
- 單精度float
- 雙精度double
兩者的區別在于表示的有效數字(包括小數點前邊的數字個數,如:3.14有效數字個數為3)范圍不同(但是在c++中一般最多顯示6位有效數字,要顯示更多的需要自己配置)。
示例:
#include <iostream>
using namespace std;
int main()
{float f1 = 3.14f;//f代表是單精度數據類型double d1 = 3.14;cout << f1 << endl;cout << d1<< endl;cout << "float sizeof = " << sizeof(f1) << endl;cout << "double sizeof = " << sizeof(d1) << endl;//科學計數法float f2 = 3e2; // 3 * 10 ^ 2 cout << "f2 = " << f2 << endl;float f3 = 3e-2; // 3 * 0.1 ^ 2cout << "f3 = " << f3 << endl;system("pause");//等待程序return 0;
}
2.4 字符型
作用:字符型變量用于顯示單個字符
語法:char ch = ‘a’;
注意1:在顯示字符型變量時,用單引號將字符括起來,不要用雙引號注意2:單引號內只能有一個字符,不可以是字符串
- C和C++中字符型變量只占用1個字節。
- 字符型變量并不是把字符本身放到內存中存儲,而是將對應的ASCII編碼放入到存儲單元
示例:
#include <iostream>
using namespace std;
int main()
{char ch = 'a';cout << ch << endl;cout << sizeof(char) << endl;//ch = "abcde"; //錯誤,不可以用雙引號//ch = 'abcde'; //錯誤,單引號內只能引用一個字符cout << (int)ch << endl; //查看字符a對應的ASCII碼ch = 97; //可以直接用ASCII給字符型變量賦值cout << ch << endl;system("pause");//等待程序return 0;
}
ASCII碼表格:
5 、轉義字符
作用:用于表示一些不能顯示出來的ASCII字符
現階段我們常用的轉義字符有:\n \ \t
#include <iostream>
using namespace std;
int main()
{cout << "\\" << endl;cout << "\tHello" << endl;cout << "\n" << endl;system("pause");//等待程序return 0;
}
6 、字符串型
作用:用于表示一串字符
兩種風格:
1、C風格字符串: char 變量名[]= “字符串值”
示例:
#include <iostream>
using namespace std;
int main() {char str1[] = "hello world";cout << str1 << endl;system("pause");return 0;
}
注意:C風格的字符串要用雙引號括起來
2、C++風格字符串: string 變量名 = “字符串值”
示例:
#include <iostream>
#include<string>
using namespace std;
int main() {string str = "hello world";cout << str << endl;system("pause");return 0;
}
注意:C++風格字符串,需要加入頭文件#include<string>
7、 布爾類型 bool
作用:布爾數據類型代表真或假的值
bool類型只有兩個值:
- true — 真(本質是1)
- false — 假(本質是0)
bool類型占1個字節大小
示例:
#include <iostream>
using namespace std;
int main() {bool flag = true;cout << flag << endl; // 1flag = false;cout << flag << endl; // 0cout << "size of bool = " << sizeof(bool) << endl; //1system("pause");return 0;
}
8 、數據的輸入
作用:用于從鍵盤獲取數據
關鍵字:cin
語法: cin >> 變量
示例:
#include <iostream>
#include<string>
using namespace std;
int main(){//整型輸入int a = 0;cout << "請輸入整型變量:" << endl;cin >> a;cout << a << endl;//浮點型輸入double d = 0;cout << "請輸入浮點型變量:" << endl;cin >> d;cout << d << endl;//字符型輸入char ch = 0;cout << "請輸入字符型變量:" << endl;cin >> ch;cout << ch << endl;//字符串型輸入string str;cout << "請輸入字符串型變量:" << endl;cin >> str;cout << str << endl;//布爾類型輸入bool flag = true;cout << "請輸入布爾型變量:" << endl;cin >> flag;cout << flag << endl;system("pause");return EXIT_SUCCESS;
}
運算符
作用:用于執行代碼的運算
本章我們主要講解以下幾類運算符:
1 、算術運算符
作用:用于處理四則運算
算術運算符包括以下符號:
示例1:
//加減乘除
#include <iostream>
using namespace std;
int main() {int a1 = 10;int b1 = 3;cout << a1 + b1 << endl;cout << a1 - b1 << endl;cout << a1 * b1 << endl;cout << a1 / b1 << endl; //兩個整數相除結果依然是整數int a2 = 10;int b2 = 20;cout << a2 / b2 << endl; int a3 = 10;int b3 = 0;//cout << a3 / b3 << endl; //報錯,除數不可以為0//兩個小數可以相除double d1 = 0.5;double d2 = 0.25;cout << d1 / d2 << endl;system("pause");return 0;
}
總結:在除法運算中,除數不能為0
示例2:
//取模
#include <iostream>
using namespace std;
int main() {int a1 = 10;int b1 = 3;cout << 10 % 3 << endl;int a2 = 10;int b2 = 20;cout << a2 % b2 << endl;int a3 = 10;int b3 = 0;//cout << a3 % b3 << endl; //取模運算時,除數也不能為0//兩個小數不可以取模double d1 = 3.14;double d2 = 1.1;//cout << d1 % d2 << endl;system("pause");return 0;
}
總結:只有整型變量可以進行取模運算
示例3:
//遞增
#include <iostream>
using namespace std;
int main() {//后置遞增int a = 10;a++; //等價于a = a + 1cout << a << endl; // 11//前置遞增int b = 10;++b;cout << b << endl; // 11//區別//前置遞增先對變量進行++,再計算表達式int a2 = 10;int b2 = ++a2 * 10;cout << b2 << endl;//后置遞增先計算表達式,后對變量進行++int a3 = 10;int b3 = a3++ * 10;cout << b3 << endl;system("pause");return 0;
}
總結:前置遞增先對變量進行++,再計算表達式,后置遞增相反
2、賦值運算符
作用:用于將表達式的值賦給變量
示例:
#include <iostream>
using namespace std;
int main() {//賦值運算符//=int a = 10;a = 100;cout << "a = " << a << endl;// +=a = 10;a += 2; // a = a + 2;cout << "a = " << a << endl;// -=a = 10;a -= 2; // a = a - 2cout << "a = " << a << endl;// *=a = 10;a *= 2; // a = a * 2cout << "a = " << a << endl;// /=a = 10;a /= 2; // a = a / 2;cout << "a = " << a << endl;// %=a = 10;a %= 2; // a = a % 2;cout << "a = " << a << endl;system("pause");return 0;
}
3 、比較運算符
作用:用于表達式的比較,并返回一個真值或假值
比較運算符有以下符號:
示例:
#include <iostream>
using namespace std;
int main() {int a = 10;int b = 20;cout << (a == b) << endl; // 0 cout << (a != b) << endl; // 1cout << (a > b) << endl; // 0cout << (a < b) << endl; // 1cout << (a >= b) << endl; // 0cout << (a <= b) << endl; // 1system("pause");return 0;
}
4、 邏輯運算符
作用:用于根據表達式的值返回真值或假值
邏輯運算符有以下符號:
示例1:邏輯非
#include <iostream>
using namespace std;
//邏輯運算符 --- 非
int main() {int a = 10;cout << !a << endl; // 0cout << !!a << endl; // 1system("pause");return 0;
}
總結: 真變假,假變真
示例2:邏輯與
#include <iostream>
using namespace std;
//邏輯運算符 --- 與
int main() {int a = 10;int b = 10;cout << (a && b) << endl;// 1a = 10;b = 0;cout << (a && b) << endl;// 0 a = 0;b = 0;cout << (a && b) << endl;// 0system("pause");return 0;
}
總結:邏輯與運算符總結: 同真為真,其余為假
示例3:邏輯或
#include <iostream>
using namespace std;
//邏輯運算符 --- 或
int main() {int a = 10;int b = 10;cout << (a || b) << endl;// 1a = 10;b = 0;cout << (a || b) << endl;// 1 a = 0;b = 0;cout << (a || b) << endl;// 0system("pause");return 0;
}
邏輯或運算符總結: 同假為假,其余為真
程序流程結構
C/C++支持最基本的三種程序運行結構:順序結構、選擇結構、循環結構
- 順序結構:程序按順序執行,不發生跳轉
- 選擇結構:依據條件是否滿足,有選擇的執行相應功能
- 循環結構:依據條件是否滿足,循環多次執行某段代碼
1 、選擇結構
1.1 if語句
作用:執行滿足條件的語句
if語句的三種形式
- 單行格式if語句
- 多行格式if語句
- 多條件的if語句
①單行格式if語句:if(條件){ 條件滿足執行的語句 }
示例:
#include <iostream>
using namespace std;
int main() {//選擇結構-單行if語句//輸入一個分數,如果分數大于600分,視為考上一本大學,并在屏幕上打印int score = 0;cout << "請輸入一個分數:" << endl;cin >> score;cout << "您輸入的分數為: " << score << endl;//if語句//注意事項,在if判斷語句后面,不要加分號if (score > 600){cout << "我考上了一本大學!!!" << endl;}system("pause");return 0;
}
注意:if條件表達式后不要加分號
②多行格式if語句:if(條件){ 條件滿足執行的語句 }else{ 條件不滿足執行的語句 };
示例:
#include <iostream>
using namespace std;
int main() {int score = 0;cout << "請輸入考試分數:" << endl;cin >> score;if (score > 600){cout << "我考上了一本大學" << endl;}else{cout << "我未考上一本大學" << endl;}system("pause");return 0;
}
③多條件的if語句:if(條件1){ 條件1滿足執行的語句 }else if(條件2){條件2滿足執行的語句}… else{ 都不滿足執行的語句}
示例:
#include <iostream>
using namespace std;int main() {int score = 0;cout << "請輸入考試分數:" << endl;cin >> score;if (score > 600){cout << "我考上了一本大學" << endl;}else if (score > 500){cout << "我考上了二本大學" << endl;}else if (score > 400){cout << "我考上了三本大學" << endl;}else{cout << "我未考上本科" << endl;}system("pause");return 0;
}
嵌套if語句:在if語句中,可以嵌套使用if語句,達到更精確的條件判斷
案例需求:
- 提示用戶輸入一個高考考試分數,根據分數做如下判斷
- 分數如果大于600分視為考上一本,大于500分考上二本,大于400考上三本,其余視為未考上本科;
- 在一本分數中,如果大于700分,考入北大,大于650分,考入清華,大于600考入人大。
示例:
#include <iostream>
using namespace std;
int main() {int score = 0;cout << "請輸入考試分數:" << endl;cin >> score;if (score > 600){cout << "我考上了一本大學" << endl;if (score > 700){cout << "我考上了北大" << endl;}else if (score > 650){cout << "我考上了清華" << endl;}else{cout << "我考上了人大" << endl;}}else if (score > 500){cout << "我考上了二本大學" << endl;}else if (score > 400){cout << "我考上了三本大學" << endl;}else{cout << "我未考上本科" << endl;}system("pause");return 0;
}
1.2 三目運算符
作用: 通過三目運算符實現簡單的判斷
語法:表達式1 ? 表達式2 :表達式3
解釋:
如果表達式1的值為真,執行表達式2,并返回表達式2的結果;
如果表達式1的值為假,執行表達式3,并返回表達式3的結果。
三目運算符返回的是變量,可以繼續賦值
示例:
#include <iostream>
using namespace std;
int main() {int a = 10;int b = 20;int c = 0;c = a > b ? a : b;cout << "c = " << c << endl;//C++中三目運算符返回的是變量,可以繼續賦值(a > b ? a : b) = 100;cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;system("pause");return 0;
}
總結:和if語句比較,三目運算符優點是短小整潔,缺點是如果用嵌套,結構不清晰
1.3 switch語句
作用:執行多條件分支語句
語法:
switch(表達式){case 結果1:執行語句;break;case 結果2:執行語句;break;...default:執行語句;break;}
示例:
#include <iostream>
using namespace std;
int main() {//請給電影評分 //10 ~ 9 經典 // 8 ~ 7 非常好// 6 ~ 5 一般// 5分以下 爛片int score = 0;cout << "請給電影打分" << endl;cin >> score;switch (score){case 10:case 9:cout << "經典" << endl;break;case 8:cout << "非常好" << endl;break;case 7:case 6:cout << "一般" << endl;break;default:cout << "爛片" << endl;break;}system("pause");return 0;
}
注意1:switch語句中表達式類型只能是整型或者字符型
注意2:case里如果沒有break,那么程序會一直向下執行
總結:與if語句比,對于多條件判斷時,switch的結構清晰,執行效率高,缺點是switch不可以判斷區間
2、 循環結構
2.1 while循環語句
作用:滿足循環條件,執行循環語句
語法:while(循環條件){ 循環語句 }
解釋:只要循環條件的結果為真,就執行循環語句
示例:
#include <iostream>
using namespace std;
int main() {int num = 0;while (num < 10){cout << "num = " << num << endl;num++;}system("pause");return 0;
}
注意:在執行循環語句時候,程序必須提供跳出循環的出口,否則出現死循環
2.2 do…while循環語句
作用: 滿足循環條件,執行循環語句
語法: do{ 循環語句 } while(循環條件);
注意:與while的區別在于do…while會先執行一次循環語句,再判斷循環條件
示例:
#include <iostream>
using namespace std;
int main() {int num = 0;do{cout << num << endl;num++;} while (num < 10); system("pause");return 0;
}
總結:與while循環區別在于,do…while先執行一次循環語句,再判斷循環條件
2.3 for循環語句
作用: 滿足循環條件,執行循環語句
語法:for(起始表達式;條件表達式;末尾循環體) { 循環語句; }
示例:
#include <iostream>
using namespace std;
int main() {for (int i = 0; i < 10; i++){cout << i << endl;}system("pause");return 0;
}
注意:for循環中的表達式,要用分號進行分隔
總結:while , do…while, for都是開發中常用的循環語句,for循環結構比較清晰,比較常用
2.4 嵌套循環
作用: 在循環體中再嵌套一層循環,解決一些實際問題
示例:
#include <iostream>
using namespace std;
int main() {//外層循環執行1次,內層循環執行1輪for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){cout << "*" << " ";}cout << endl;}system("pause");return 0;
}
3 、跳轉語句
3.1 break語句
作用: 用于跳出選擇結構或者循環結構
break使用的時機:
- 出現在switch條件語句中,作用是終止case并跳出switch
- 出現在循環語句中,作用是跳出當前的循環語句
- 出現在嵌套循環中,跳出最近的內層循環語句
示例1:
#include <iostream>
using namespace std;
int main() {//1、在switch 語句中使用breakcout << "請選擇您挑戰副本的難度:" << endl;cout << "1、普通" << endl;cout << "2、中等" << endl;cout << "3、困難" << endl;int num = 0;cin >> num;switch (num){case 1:cout << "您選擇的是普通難度" << endl;break;case 2:cout << "您選擇的是中等難度" << endl;break;case 3:cout << "您選擇的是困難難度" << endl;break;}system("pause");return 0;
}
示例2:
#include <iostream>
using namespace std;
int main() {//2、在循環語句中用breakfor (int i = 0; i < 10; i++){if (i == 5){break; //跳出循環語句}cout << i << endl;}system("pause");return 0;
}
示例3:
#include <iostream>
using namespace std;
int main() {//在嵌套循環語句中使用break,退出內層循環for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if (j == 5){break;}cout << "*" << " ";}cout << endl;}system("pause");return 0;
}
3.2 continue語句
作用:在循環語句中,跳過本次循環中余下尚未執行的語句,繼續執行下一次循環
示例:
#include <iostream>
using namespace std;
int main() {for (int i = 0; i < 100; i++){if (i % 2 == 0){continue;}cout << i << endl;}system("pause");return 0;
}
注意:continue并沒有使整個循環終止,而break會跳出循環
3.3 goto語句
作用:可以無條件跳轉語句
語法: goto 標記
解釋:如果標記的名稱存在,執行到goto語句時,會跳轉到標記的位置
示例:
#include <iostream>
using namespace std;
int main() {cout << "1" << endl;goto FLAG;cout << "2" << endl;cout << "3" << endl;cout << "4" << endl;FLAG:cout << "5" << endl;system("pause");return 0;
}
注意:在程序中不建議使用goto語句,以免造成程序流程混亂