# c/c++ 簡單小小整理 ## 變數與常數 `#define i 50` 宣告不改變值\ `int` a=0; 整數宣告\ `char` a='0'; 宣告字元\ `float` a=0.0; 宣告浮點數\ `double` a=0.0; 宣告浮點數\ `bool` a=false; 宣告布林值\ `&&` AND\ `||` OR\ `^ `XOR\ `! `NOT\ `%` 取商數 ## 迴圈與判斷 `%d` 十進制整數\ `%o` 八進制整數\ `%x` 十六進制整數\ `%c` 字元\ `%s` 字串\ if(條件){`true`}\ else{`false`}\ switch(值)\ {\ case 值:\ `true`\ break;\ default:\ `false`\ }\ for(`初始值`;`條件`;`遞增遞減`)\ while(`條件`) ## 陣列 `int a[max];` 一維\ `int a[max][max];` 二維\ 陣列從零開始\ `char a[max];` 字串\ 字串存的值為ASCII\ `1` 代表 49\ `A` 代表 65\ `a` 代表 97\ `#include<ctype.h>`\ `islower()` 判斷字母是否為小寫\ `issupper()` 判斷字母是否為大寫\ `#include<string.h>`\ `strlen()` 字串長度 包含`\0` ## 指標與結構與串列 `int *a;`指標宣告\ `&a` a的記憶體位址 `結構宣告` ``` struct a { /*宣告成員*/ int id; char name[max]; } ``` `struct a obj;`宣告物件\ `obj.id;`引用物件 *** `單向節點` 新節點插入第一個節點前,成為此串首節點\ `newnode->next=first;`\ `first-=newnode;`\ 新節點插入最後一個節點\ `ptr->next=newnode;`\ `newnode->next=NULL;`\ 新節點插入中間位置\ `newnode->next=x->next;`\ `x->next=newnode;`\ 刪除第一節點\ `top=head;`\ `head=head->next;`\ `free(top);`\ 刪除最後節點\ `ptr->next=tail;`\ `ptr->next=NULL;`\ `free(tail);`\ 刪除中間節點\ `Y=ptr->next;`\ `ptr->next=Y->next;`\ `free(Y);` *** `環狀串列`\ 新節點插入第一個節點前,成為此串首節點\ `x->next=head;`\ `CurNode=head;`\ `while(CurNode->next!=head)`\ `CurNode=CurNode->next;`\ `CurNode->next=x;`\ `head=x;`\ 新節點X插入任意節點I\ `X->next=I->next;`\ `I->=X;`\ 刪除節點\ `CurNode=head;`\ `while(CurNode->next!=head)`\ `CurNode=CurNode->next;`\ `TaiNode=CurNode;`\ `head=head->next;`\ `TailNode->next=head;` ## 函數 `傳值呼叫` ``` int f(int i) {  return i+1; } /*跟f(x)=x+1一樣*/ ``` `傳址呼叫` ``` int f(*i); int f(int &i) { return i+1; } ``` `遞迴` ``` int f(int i) { if(i>0) { return f(i-1)+i; } else { return 0; } } /*類迴圈 自己回傳自己*/ ``` ## 檔案與排序 `檔案`\ `FILE *fp;` 宣告檔案\ `fopen("input.txt","r");` 開啟檔案\ 檔案位置要寫絕對位置\ `r` 讀取\ `w` 寫入\ `a` 新增\ `fscanf(fp,"%d",&a);`檔案資料寫入變數\ `fclose(fp);`關閉檔案 `strcmp();` 字串比較 `轉換式` ``` int temp; temp = a; a = b; b = temp; ``` 將A和B做交換 ## 堆疊、佇列 `堆疊`\ `先進後出`\ `判斷是否為空堆疊` ``` int isEmpty() { if(top==-1) { return 1; } else { return 0; } } ``` `存放頂端資料` ``` int push(int data) { if(top>=MAXSTACK) { /*FALL*/ return 0; } else { /*ADD*/ return 1; } } ``` `堆疊` ``` int pop() { if(isEmpty()) { return -1; } else { return stack[top--]; } } ``` *** `佇列`\ `先進先出`\ `新增` ``` void enqueue(int item) { if(rear==MAX_SIZE-1) { /*FALL*/ } else { rear++; queue(rear)=item; } } ``` `刪除` ``` void enqueue(int item) { if(front==rear) { /*FULL*/ } else { front++; item=Queue[front]; } } ``` `回傳` ``` void FRONT_VALUE(int *QUEUE) { if(front==reer) { /*FULL*/ } else { /*輸出*/ } } ``` ## vector容器 `#include<vector>` 引用標頭檔\ `vector.begin()` 容器的開始\ `vector.end()` 容器的尾端\ `vector.push_back()` 加入值到容器尾端\ `vector.pop_back()` 移除尾端的值\ `vector.insert(,)` 新增值\ `vector.clear()` 清空容器\ `vector.erase()` 刪除值\ `vector.capacity()` 容量大小\ `vector.size()` 長度大小\ `vector.reserve()` 預設大小\ `vector.resize()` 重新指定大小\ `vector.front()` 讀取第一個值\ `vector.back()` 讀取最後一個值\ `vector.empty()` 判斷容器是否為空\ `vector.swap(vector1)` 容器互換 ## map容器 `#include<map>` 引用標頭檔\ `map.begin()` 容器的開始\ `map.end()` 容器的尾端\ `map.insert(,)` 新增值\ `map.erase()` 刪除值\ `map.clear()` 清空容器\ `map.size()` 長度大小\ `map.count()` 指定元素出現次數\ `map.empty()` 判斷容器是否為空\ `map.find()` 查找元素
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up