開始進行演算法筆記的學習,在此紀錄下來,同時作為自己日后復習資料,
1.基本語法
#include <iostream>
using namespace std;
int main(){
cout<<"hello"<<endl;
return 0;
}
以上是最基礎的一個代碼端:
引入頭檔案:#include <iostream>
引入命名空間(相當于2個不同的人可以擁有同一件物品,并且使用的時候沒有沖突):using namespace std
main():相當于程式的入口
cout/cin:C++中用于輸出和輸入,類似于C語言中的printf和scanf,后面詳細介紹
2.C++中資料型別
#include<iostream> 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; }
結果顯示
type: ************size************** bool: 所占位元組數:1 最大值:1 最小值:0 char: 所占位元組數:1 最大值: 最小值:? signed char: 所占位元組數:1 最大值: 最小值:? unsigned char: 所占位元組數:1 最大值:? 最小值: wchar_t: 所占位元組數:4 最大值:2147483647 最小值:-2147483648 short: 所占位元組數:2 最大值:32767 最小值:-32768 int: 所占位元組數:4 最大值:2147483647 最小值:-2147483648 unsigned: 所占位元組數:4 最大值:4294967295 最小值:0 long: 所占位元組數:8 最大值:9223372036854775807 最小值:-9223372036854775808 unsigned long: 所占位元組數:8 最大值:18446744073709551615 最小值:0 double: 所占位元組數:8 最大值:1.79769e+308 最小值:2.22507e-308 long double: 所占位元組數:16 最大值:1.18973e+4932 最小值:3.3621e-4932 float: 所占位元組數:4 最大值:3.40282e+38 最小值:1.17549e-38 size_t: 所占位元組數:8 最大值:18446744073709551615 最小值:0 string: 所占位元組數:24 type: ************size**************
3.定義變數以及型別
定義變數:
int d = 3, f = 5; // d 和 f 的宣告 int d = 3, f = 5; // 定義并初始化 d 和 f byte z = 22; // 定義并初始化 z char x = 'x'; // 變數 x 的值為 'x'
變數的型別間是可以互相轉換的,轉換又分為自動轉換和強制轉換,
自動轉換規則:
- 1、若參與運算量的型別不同,則先轉換成同一型別,然后進行運算,
- 2、轉換按資料長度增加的方向進行,以保證精度不降低,如int型和long型運算時,先把int量轉成long型后再進行運算, a、若兩種型別的位元組數不同,轉換成位元組數高的型別 b、若兩種型別的位元組數相同,且一種有符號,一種無符號,則轉換成無符號型別
- 3、所有的浮點運算都是以雙精度進行的,即使僅含float單精度量運算的運算式,也要先轉換成double型,再作運算,
- 4、char型和short型參與運算時,必須先轉換成int型,
- 5、在賦值運算中,賦值號兩邊量的資料型別不同時,賦值號右邊量的型別將轉換為左邊量的型別,如果右邊量的資料型別長度比左邊長時,將丟失一部分資料,這樣會降低精度
4.作用域
區域變數:區域變數宣告后其宣告周期僅僅在定義的該區域空間內有效(即{ }之間)
int main () { // 區域變數宣告 int a, b; int c; // 實際初始化 a = 10; b = 20; c = a + b; return 0; }
全域變數:在整個定義域內均有效果,直至程式結束才消亡
#include <iostream> using namespace std; // 全域變數宣告 int g;
形式引數:用于傳遞引數,將函式之外的數值傳遞進函式內部
小總結:
在程式中,區域變數和全域變數的名稱可以相同,但是在函式內的區域變數與全域變數是兩個獨立的變數,互不影響,
當變數間出現重名的情況下,作用域小的屏蔽作用域大的,
存盤在靜態資料區的變數會在程式剛開始運行時就完成初始化,也是唯一的一次初始化(static),
5.C++運算子
主要講&、|、!,其中&&與||具有短路特性,
短路特性:即當兩者之間進行&&或者||運算的時候,如果其中一者已經明確錯誤或者正確,則后面不在運行,
先判斷左邊的值是否為真,
如果為假,那么整個運算式毫無疑問也為假,
如果為真,那就還需要判斷右值,才能知道整個式子的值,
這個時候判斷右值的程序就起了一個if的作用,可以利用這個程序判斷右邊運算式是否為真,
舉例說明:
&用法:只有2個都為1,那么結果是1,否則為0:1&1=1,1&0=0,0&0=0,0&1=0;
|用法:只要有一個是1,那么結果為1,否則為0:1&1=1,1&0=1,0&0=0,0&1=1;
6.C++回圈、判斷、函式
一、回圈:
| while 回圈 | 當給定條件為真時,重復陳述句或陳述句組,它會在執行回圈主體之前測驗條件, |
| for 回圈 | 多次執行一個陳述句序列,簡化管理回圈變數的代碼, |
| do...while 回圈 | 除了它是在回圈主體結尾測驗條件外,其他與 while 陳述句類似, |
| 嵌套回圈 | 您可以在 while、for 或 do..while 回圈內使用一個或多個回圈, |
一般來說,回圈分為回圈條件、回圈體和結束條件,
如果一個回圈沒有結束條件,那么可能變成死回圈,死回圈極其耗費記憶體,因此寫代碼一定記得加結束條件,
遞回類似于一個不斷嵌套的回圈,因此由遞回體、結束時跳出的條件,但是遞回由于嵌套太多,并且大多執行的都是重復的代碼,因此推薦使用尾遞回,個人感覺極大的減少了記憶體消耗,
二、判斷:
| if 陳述句 | 一個 if 陳述句 由一個布爾運算式后跟一個或多個陳述句組成, |
| if...else 陳述句 | 一個 if 陳述句 后可跟一個可選的 else 陳述句,else 陳述句在布爾運算式為假時執行, |
| 嵌套 if 陳述句 | 您可以在一個 if 或 else if 陳述句內使用另一個 if 或 else if 陳述句, |
| switch 陳述句 | 一個 switch 陳述句允許測驗一個變數等于多個值時的情況, |
| 嵌套 switch 陳述句 | 您可以在一個 switch 陳述句內使用另一個 switch 陳述句, |
判斷感覺主要注意的就是switch陳述句,因為一般對于switch陳述句都不太熟悉,
switch陳述句:
switch(expression){ case constant-expression : statement(s); break; // 可選的 case constant-expression : statement(s); break; // 可選的 // 您可以有任意數量的 case 陳述句 default : // 可選的 statement(s); }
switch陳述句規則:
- switch 陳述句中的 expression 必須是一個整型或列舉型別,或者是一個 class 型別,其中 class 有一個單一的轉換函式將其轉換為整型或列舉型別,
- 在一個 switch 中可以有任意數量的 case 陳述句,每個 case 后跟一個要比較的值和一個冒號,
- case 的 constant-expression 必須與 switch 中的變數具有相同的資料型別,且必須是一個常量或字面量,
- 當被測驗的變數等于 case 中的常量時,case 后跟的陳述句將被執行,直到遇到 break 陳述句為止,
- 當遇到 break 陳述句時,switch 終止,控制流將跳轉到 switch 陳述句后的下一行,
- 不是每一個 case 都需要包含 break,如果 case 陳述句不包含 break,控制流將會 繼續 后續的 case,直到遇到 break 為止,
- 一個 switch 陳述句可以有一個可選的 default case,出現在 switch 的結尾,default case 可用于在上面所有 case 都不為真時執行一個任務,default case 中的 break 陳述句不是必需的,
三、函式
函式定義:
函式是一組一起執行一個任務的陳述句,每個 C++ 程式都至少有一個函式,即主函式 main() ,所有簡單的程式都可以定義其他額外的函式,
可以把代碼劃分到不同的函式中,如何劃分代碼到不同的函式中是由您來決定的,但在邏輯上,劃分通常是根據每個函式執行一個特定的任務來進行的,
函式宣告告訴編譯器函式的名稱、回傳型別和引數,函式定義提供了函式的實際主體,
格式:
return_type function_name( parameter list )
{
body of the function
}
Lambda函式:
// 定義簡單的lambda運算式 auto basicLambda = [] { cout << "Hello, world!" << endl; }; // 呼叫 basicLambda(); // 輸出:Hello, world!
上述是一個Lambda函式小例子:
[ ] 是 lambda 引出符,編譯器根據該引出符判斷接下來的代碼是否是 lambda 函式,
{ }:函式體,內容與普通函式一樣,不過除了可以使用引數之外,還可以使用所有捕獲的變數
7.C++陣列、字串
陣列:
靜態 int array[100]; 定義了陣列 array,并未對陣列進行初始化
靜態 int array[100] = {1,2}; 定義并初始化了陣列 array
動態 int* array = new int[100]; delete []array; 分配了長度為 100 的陣列 array
動態 int* array = new int[100]{1,2}; delete []array; 為長度為100的陣列array并且初始化前兩個元素
字串:
定義字串:char str[11] = "Hello";
string類提供了一系列針對字串的操作,比如:
- 1. append() -- 在字串的末尾添加字符
- 2. find() -- 在字串中查找字串
- 4. insert() -- 插入字符
- 5. length() -- 回傳字串的長度
- 6. replace() -- 替換字串
- 7. substr() -- 回傳某個子字串
- 8. ...
實體如下:
#include <iostream> #include <string> using namespace std; int main() { //定義一個string類物件 string http = "www.runoob.com"; //列印字串長度 cout<<http.length()<<endl; //拼接 http.append("/C++"); cout<<http<<endl; //列印結果為:www.runoob.com/C++ //洗掉 int pos = http.find("/C++"); //查找"C++"在字串中的位置 cout<<pos<<endl; http.replace(pos, 4, ""); //從位置pos開始,之后的4個字符替換為空,即洗掉 cout<<http<<endl; //找子串runoob int first = http.find_first_of("."); //從頭開始尋找字符'.'的位置 int last = http.find_last_of("."); //從尾開始尋找字符'.'的位置 cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并列印 return 0; }
8.C++基本輸入輸出

基本上使用iostream這個頭檔案
標準輸出流cout:
#include <iostream> using namespace std; int main( ) { char str[] = "Hello C++"; cout << "Value of str is : " << str << endl; } 結果:Value of str is : Hello C++
標準輸入流cin:
#include <iostream> using namespace std; int main( ) { char name[50]; cout << "請輸入您的名稱: "; cin >> name; cout << "您的名稱是: " << name << endl; } 結果: 請輸入您的名稱: cplusplus 您的名稱是: cplusplus
個人剛剛開始寫博客,有錯誤的地方麻煩指出,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/110907.html
標籤:其他
上一篇:一文解讀ARM架構 (轉)
下一篇:《演算法筆記》之入門模擬
