>
> 內容若有疑慮或錯誤歡迎糾正
> 註 : 此筆記並**不包含演算法**
## 程式快億點點
>[!Warning]溫馨提醒
>1. debug 時最好先反白不然會看不到輸出
>2. cin/cout 與 printf/scanf 不要混用,輸入/出會有問題
```cpp
ios_base::sync_with_stdio(0); // 取消 cin,cout & printf&scanf 的 I/O 同步
cin.tie(0); // 取消 cin & cout 綁定,緩衝區不會滿
```
[參考資料連結 : cin.tie 與 sync_with_stdio 加速輸入輸出](https://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html)
[參考資料連結 : Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull)
[參考資料連結 : ends,flush,endl 用法區別](https://blog.csdn.net/u013240812/article/details/20618883)
[參考資料連結 : std::endl ??](https://hackmd.io/@magical/ryMaPoaQ_)
[參考資料連結 : "std::endl" vs "\n"](https://stackoverflow.com/questions/213907/stdendl-vs-n)
### 萬用標頭檔
```cpp=
#include <bits/stdc++.h>
```
### 其他
[**黑魔法**](https://github.com/mysh212/Coding/blob/master/library/fast) 勝浩學長介紹的 **但其實我不知道那是什麼原理**
# 基本
## 運算式
**運算**|**式子**|**程式**
--|--|--
**平方** |$x^y$|`pow(x,y)`
**根號** |$√x$|`sqrt(x)`
**指數** |$10^3+1$|`a=1e3 + 1;`
**數字處理**|**程式**
--|--
**變成正數** |`abs(x)`
**無條件進位** |`ceil(x)`
**無條件捨去** |`floor(x)`
**四捨五入** |`round (x)`
**其他**|**程式**
--|--
**排序**|`sort()`
**延遲 n 豪秒** |`_sleep(n)`
**清除cmd上的資料**|`clrscr()`
[參考資料連結 : C++ 如何清除屏幕上的内容?](https://blog.csdn.net/Code_easy/article/details/126678558)
[參考資料連結 : C++ std::sort 排序用法與範例完整介紹](https://shengyu7697.github.io/std-sort/)
[參考資料連結 : 內建 sort 與比較函式](https://hackmd.io/@sa072686/cp/%2F%40sa072686%2FB1Zg7tI5U)
## 隨機取數
```cpp
mt19937 randomNum(time_t(0));
randomNum()
```
[參考資料連結 : C++ STL mt19937 使用说明](https://www.jianshu.com/p/6d9a7de995bb)
[參考資料連結 : [C++11] C++11 帶來的隨機數產生器](https://www.cnblogs.com/egmkang/archive/2012/09/06/2673253.html)
## cin & cout
### 基本
```cpp
cout << 變數 << "字串" << '字元' ;
```
:::spoiler **ex :**
```cpp=
cout << "Meow";
```
--> Meow
:::
### 如何控制輸出的字元數?
```cpp
setw(輸出的數字)
```
:::spoiler **ex :**
```=
cout<< setw(4) << 'a';
```
--> [ ][ ][ ]a
(前有三個空格)
:::
### 如何改變填入的資料?
```cpp
setfill('你想填入的資料')
```
:::spoiler **ex :**
```cpp=
cout<< setfill('0')<< setw(4)<< 'a';
```
--> 000a
:::
### 如何改成由左開始?
```
left // 只要在setw前就可以
```
:::spoiler **ex :**
```cpp=
cout << left << setfill('0') << setw(4) << 'a' ;
```
--> a000
:::
## printf & scanf
### 常用
`\n` 換行 跟 `cout<<endl;` 一樣,==要習慣用 `\n`==,以後才不會出問題
`\t` 就是 <kbd>Tab</kbd>
`\r` 換到句首
### 控制符
| **格式控制符** | | **說明** |
| ---- | ---- | --- |
| <font color=red>%c | |<font color=red>字元 |
| <font color=red>%s | |<font color=red>字串|
| <font color=red>%lu | |<font color=red>long unsigned|
| <font color=red>%p | |<font color=red>指標|
|<font color=orange>%hd、%d、%ld|<font color=orange>%hu、%u、%lu|<font color=orange>十進制|
|<font color=orange>%ho、%o、%lo|<font color=orange>%#ho、%#o、%#lo|<font color=orange>八進制
|<font color=orange>%hx、%x、%lx、%hX、%X、%lX|<font color=orange>%#hx、%#x、%#lx、%#hX、%#X、%#lX|<font color=orange>十六進制|
|<font color=gree>%f、%lf||<font color=gree>十進制小數
|<font color=gree>%e、%le|<font color=gree>%E、%lE|<font color=gree>指數小數(科學記號)|
|<font color=gree>%g、%lg|<font color=gree>%G、%lG|<font color=gree>上面兩個小數取比較短的|
|<font color=snake>%* | |<font color=snake>預留一個字元寬度|
>[!Note]
`%`、 `\`、 `"` 如何打出來
>```cpp=
>printf(" %% \n");
>printf(" \" \n");
>printf(" \\ ");
>```
> --> %
> "
> \
:::spoiler **符號意思**
字符|對應
--|--
**橘色符號**|**資料結構**
<font color=orange>%h_|short
<font color=orange>%_|int
<font color=orange>%l_|long
<font color=orange>#|有前綴
**橘色字母**|**意思**
<font color=orange>d|十進位
<font color=orange>u|十進位
<font color=orange>o|八進位
<font color=orange>x|十六進位 (小寫)
<font color=orange>X|十六進位 (大寫)
**綠色符號**|**資料結構**
<font color=gree>%_|float
<font color=gree>%l_|double
**綠色字母**|**意思**
<font color=gree>f|十進位
<font color=gree>e|指數 (e小寫)
<font color=gree>E|指數 (E大寫)
<font color=gree>g|十進位/指數 取小 (e小寫)
<font color=gree>G|十進位/指數 取小 (E大寫)
:::
[參考資料連結 : printf 用法大全,C語言 printf 格式控制符一覽表](https://c.biancheng.net/view/159.html)
[參考資料連結 : printf 與 scanf](https://openhome.cc/Gossip/CGossip/PrintfScanf.html)
## string
### 用法
程式 | 說明
--|--
`=` |將前面的字串變成跟後面一樣
`==` |比較兩個字串的是否相同。
`+` |連接兩個字串。
`[]`、`str.at()` |字串的第幾項字元,at 帶邊界檢查。
`str.size()`、`str.length()` |字串長度
`str.empty()` |字串是否為空
### 進階用法
符號 | 說明
--|--
`.assign(str, strn, n)` |從 str 的第 strn 個字元取出 n 個字元==設成另一個字串==
`.append(str, strn, n)` |從 str 的第 strn 個字元取出 n 個字元==加在另一字串之後==
`.find(str, n)` |從 str 的第 n 個字元==尋找是否有符合 str 的子字串==
`.insert(strn, str2)` |將 str2 ==插入第 strn 個字元後面==
`.toupper(s[i])`|將 s[i] ==轉成大寫==
[參考資料連結 : C/C++ - String 用法與心得完全攻略](https://www.mropengate.com/2015/07/cc-string-stl.html)
[參考資料連結 : 使用 string](https://openhome.cc/Gossip/CppGossip/string2.html)
## 陣列 array
```cpp
// 可以將 string 加入 char[ ]
string name = "meowoyo";
char str[ ] = "John";
name = str;
// 但 char[ ] 大小不能小於string
char str[ ] = "John";
string name = "meowoyo";
str = name; // error
```
[參考資料連結 : 字元陣列與字串](https://openhome.cc/Gossip/CppGossip/string1.html)
[參考資料連結 : ASCII 對照表](https://shihyu.github.io/books/apas01.html)
## define
將A定義為B,所以之後的程式都可以直接用A代替B
```cpp!
#define A B
```
:::spoiler **ex:**
```cpp=
#define TRUE 1 // 之後要是出現TRUE都代表1
if(TRUE)cout<<TRUE;
```
--> 1
:::
[參考資料連結 : C/C++ define 用法與範例](https://shengyu7697.github.io/cpp-define/)
## struct
### 用法一
```cpp
struct X // X表示它所包含的元素(功能像是int,string......)
{
int 名稱1;
char 名稱2;
......
}變數名稱;//如果只要一個變數可以將變數寫在這裡
//也可以而外寫成 : X 變數名稱;
```
:::spoiler **ex :**
```cpp=
struct stu
{
int age;
char name[32];
};
stu s1 = {16, "Meowyo"};
printf("%s is %d-year-old cat",s1.name,s1.age);
```
--> Meowyo is 16-year-old cat
:::
### 用法二
```cpp
X[ ].名稱n // 加入 .名稱n 代表名稱所對應的值
```
:::spoiler **ex :**
```cpp=
struct
{
int a;
int b;
} M[3];
M[1].a = 1; M[1].b = 3;
M[2].a = 3; M[2].b = 4;
M[1].a = M[2].b;
```
--> 代表 M[1].a 變成 M[2].b (如下表)
| **運行前** | M[1] | M[2]
| -- | -- | --
|**a**|<font color=red>1|2
|**b**|3|4
| **運行後** | M[1] | M[2] |
| -- | -- | -- |
|**a**|<font color=red>4|2|
|**b**|3|4|
:::
[參考資料連結 : C/C++ struct 用法與範例](https://shengyu7697.github.io/cpp-struct/)
[參考資料連結 : C++ 修饰符类型| 菜鸟教程](https://www.runoob.com/cplusplus/cpp-modifier-types.html)
## 迴圈
### while
```cpp!
while(判斷式)
{
運算式;
}
```
:::spoiler **流程圖 :**
```mermaid
graph LR;
while-->判斷式
判斷式--if true--->運算式
判斷式--if false--->結束
運算式-->判斷式
```
:::
:::spoiler **ex :**
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int a=5;
while(a>0)
{
for(int i=0; i<a;i++)
cout<<' ';
for(int i=11-a; i>a;i--)
cout<<'*';
cout<<'\n';
a--;
}
return 0;
}
```
--> *
***
*****
*******
*********
:::
### for
```cpp
for(1.初始值; 2.判斷式; 3.對初始值做運算)
{
4.運算式;
}
```
如果 ==3.對初始值做運算== 已經在 ==4.運算式== 裡時,可省略 :
```cpp
for(1.初始值; 2.判斷式)
{
4.運算式;
3.對初始值做運算;
}
```
:::spoiler **流程圖 :**
```mermaid
graph LR;
for-->2.判斷式--if true-->4.運算式-->3.對初始值做運算
3.對初始值做運算-->2.判斷式
2.判斷式--if false-->結束
```
:::
:::spoiler **ex :**
```cpp=
#include <bits/stdc++.h>
#define w '-'
#define b 'O'
using namespace std;
int main ()
{
char a[6][7]= {
{w,b,b,w,b,b,w},
{b,w,w,b,w,w,b},
{b,w,w,w,w,w,b},
{w,b,w,w,w,b,w},
{w,w,b,w,b,w,w},
{w,w,w,b,w,w,w}};
for(int i=0; i<6; i++)
{
for(int j=0; j<7; j++)
{
cout<<a[i][j]<<' ';
}
cout<<'\n';
}
return 0;
}
```
--> - O O - O O -
O - - O - - O
O - - - - - O
- O - - - O -
- - O - O - -
- - - O - - -
:::
### 如何跳出迴圈
```cpp
break // 只會跳出目前所在的迴圈for、while......)
```
:::spoiler **流程圖 :**
```mermaid
graph LR;
while-->i
i--if i <=2 -->i++ -->i
i--if i > 2-->結束
```
:::
:::spoiler **ex:**
```cpp=
while(1)
{
if(i>2)break;
i++;
}
```
:::
## switch
```cpp
switch (變數名稱或運算式)
{
case A:
運算式一;
break;
case B:
運算式二;
break;
default:
運算式三;
break;
}
//如果case沒有break會一直跑直到碰到break
```
:::spoiler **流程圖 :**
```mermaid
graph LR;
變數名稱或運算式-->caseA--符合A-->運算式一-->break
caseA--不符合A-->caseB--符合B-->運算式二-->break
caseB--不符合B-->default-->運算式三-->break
```
:::
:::spoiler **ex :**
```cpp=
#include <bits/stdc++.h>
//#include <typeinfo>
using namespace std;
int main ()
{
int score=100;
switch(score/10)
{
case 10:
case 9:
cout<<"YES";
break;
default:
cout<<"NO";
}
return 0;
}
```
--> YES
(score/10=10,符合 case 10:
但因為 case10 後沒有break,所以會繼續跑 case 9
最後輸出YES)
:::spoiler **流程圖 :**
```mermaid
graph LR;
score-->case_10--符合10-->case_9
case_10--不符合10-->case_9--符合9-->cout_YES-->break-->return_0
case_9--不符合9-->default-->cout_NO-->return_0
```
:::
[參考資料連結 : switch 條件判斷](https://openhome.cc/Gossip/CppGossip/switchStatement.html)
# 進階
## ? : 的用法
`(判斷式 ? 動作一 : 動作二)` 像是濃縮的 `if{} else{}`
:::spoiler **流程圖 :**
```mermaid
graph LR;
判斷式--if true--->動作一
判斷式--if false--->動作二
```
:::
:::spoiler **ex :**
```cpp=
int a=1;
cout<<(a != 1 ? "yes" : "no")<<endl;
cout<<(a == 1 ? "yes" : "no")<<endl;
```
--> no
yes
:::
[參考資料連結 : C++ &、&&、 |、|| 、|=、?: 逻辑运算符用法](https://blog.csdn.net/qq_41452267/article/details/105264542)
## lower_bound() / upper_bound()
>[!Warning]注意
>記得在使用前先 `sort()` 確保是由小到大
`lower_bound(a,a+n,k)` 是搜尋第一個符合 $a_i\ge k$ 的 $i$
`upper_bound(a,a+n,k)` 是搜尋第一個符合 $a_i> k$ 的 $i$
```cpp=
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int a[10] = {1,1,2,2,2,4,5,6,7,9};
int main()
{
sort(a,a+10);
auto b = lower_bound(a, a+10, 2),
c = upper_bound(a, a+10, 2);
printf("lower_bound a[%d]=%d \n",b-a,*b);
printf("upper_bound a[%d]=%d",c-a,*c);
return 0;
}
```
--> lower_bound a[2]=2
upper_bound a[5]=4
[參考資料連結 : 求比 k 大的最小值 lower_bound/upper_bound](https://hackmd.io/@sa072686/cp/%2F%40sa072686%2FHk8wnUcWB)
## typeid()
`typeid()` 可以儲存資料結構的函數
`.name()` 可以回傳資料結構對應字母==參考資料第一個連結有對照表==
```cpp=
#include <bits/stdc++.h> // 需要 <typeinfo>
using namespace std;
int main ()
{
int a = 99;
long int b = 1e6;
char c = 'M';
const type_info &ai = typeid(a);
const type_info &bi = typeid(b);
const type_info &ci = typeid(c);
printf("a 位址 : %p | 資料結構 : %s\n", &ai, ai.name());
printf("b 位址 : %p | 資料結構 : %s\n", &bi, bi.name());
printf("c 位址 : %p | 資料結構 : %s ", &ci, ci.name());
return 0;
}
```
--> a 位址 : 000000006fd19750 | 資料結構 : i
b 位址 : 000000006fd19770 | 資料結構 : l
c 位址 : 000000006fd196f0 | 資料結構 : c
[參考資料連結 : [C++] 使用 typeid() 確認變數資料型態](https://clay-atlas.com/blog/2021/05/11/cpp-cn-typeid-check-variable-data-type/)
[參考資料連結 : c/c++ 中,关键字 typeof() 用法总结](https://juejin.cn/post/7115796606783324173)
## 位址 & 指標
運算子 `&` 是提取變數所存在的==記憶體位址== (位址)
運算子 `*` 是提取指標指向==位址的資料== (數值)
```cpp=
// 我絕對不是懶得打說明所以直接打程式
#include<bits/stdc++.h>
using namespace std;
int main()
{ int*p,x=1,y=2;
cout<<"x="<<x<<",&x="<<&x<<endl;
cout<<"y="<<y<<",&y="<<&y<<endl<<"(每個人指標的位置不一定會一樣)"<<endl;
p=&x;
cout<<"執行p=&x後,p="<<p<<";*p="<<*p<<endl;
p=&y;
cout<<"執行p=&y後,p="<<p<<";*p="<<*p<<endl;
return 0;}
```
--> x=1,&x=0x61fe14
y=2,&y=0x61fe10
(每個人指標的位置不一定會一樣)
執行p=&x後,p=0x61fe14;*p=1
執行p=&y後,p=0x61fe10;*p=2
# STL
## Vector
### 常用函式
`.push_back()` 資料==加到尾巴==
`.pop_back()` ==移除尾巴==的資料
`.insert()` ==插入==資料
`.erase()` ==移除某個位置==的資料, 也可以==移除某一段範圍==的資料
`.clear()` 清空 `Vector`
`.size()` 回傳目前長度
`.empty()` 回傳是否為空
`.at(i)` 隨機存取 索引值為i 的資料,==`at(i)` 會作邊界檢查==,越界會拋出一個例外
`.reserve()` ==預先配置大小==
`[i]` 第 i 個資料,跟==陣列一樣從 0 開始==
:::spoiler **ex :**
```cpp=
#include <bits/stdc++.h> // 需要 <vector>
using namespace std;
int main()
{
vector<int> vec; // 宣告一個放 int 的 vector
vec.push_back(1); // {1}
vec.push_back(2); // {1,2}
vec.push_back(3); // {1,2,3}
vec.push_back(4); // {1,2,3,4}
vec.pop_back(); // {1,2,3}
vec.pop_back(); // {1,2}
vec[0] = 87; // {87,2}
vector<int>::iterator it = vec.begin();
vec.insert(it+1, 7); // {87,7,2}
vec.erase(it+1); // {87,2}
// 輸出
// 長度
cout << "size : " << int(vec.size()) << endl;
// 1. 用 size()
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << "\n";
// 2. 用 iterator
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
cout << *it << " ";
cout << "\n";
// 3. 用auto,c++11 以上的版本才能用
for (auto &v : vec)
cout << v << " ";
cout << "\n";
return 0;
}
```
--> size : 2
87 2
87 2
87 2
:::
[參考資料連結 : C++ std::vector 用法與範例](https://shengyu7697.github.io/std-vector/)
[參考資料連結 : (原創) 如何正確的使用迴圈 (使用for_each) ? (C/C++) (STL) (template)](https://www.cnblogs.com/oomusou/archive/2007/05/12/744000.html)
[參考資料連結 : iterator 与 const_iterator 及 const iterator 区别 - 大气象- 博客园](https://www.cnblogs.com/greatverve/archive/2012/09/12/const-iterator.html)
## stack
### 基本函數
`.empty()` 回傳是否沒有資料
`.pop()` ==移除==最後一項
`.push()` ==新增==在最後一項
`.size()` 回傳大小
`.top()` 最後一項
~~[參考資料連結 : 堆疊 Stack - C++](https://www.csie.ntu.edu.tw/~b01902011/material.php?type=cpp&&id=1)~~ 不知道為什麼不見了 QAQ
# 其他連結
### 線上C++教學網站
+ [彰中資訊讀書會 H-ARC](https://hackmd.io/@mysh212/HARC/https%3A%2F%2Fhackmd.io%2F%40mysh212%2FHARC%2F)
+ [Yui Huang 演算法學習筆記](https://yuihuang.com/)
+ [WiwiHo 的競程筆記](https://cp.wiwiho.me/)
+ [APCS Guide](https://apcs.guide/)
+ [SA 流 C++ 競程修練心法](https://hackmd.io/@sa072686/cp/%2F%40sa072686%2FBkTJ0imPB)
+ [從零開始的演算法競賽入門教學](https://emanlaicepsa.github.io/)
+ [C++超級菜鳥也可以懂的物件導向攻略 系列](https://ithelp.ithome.com.tw/users/20152702/ironman/5908)
+ [C++ Miner ── 礦坑系列](https://hackmd.io/@Mes/Cpp_Miner/https%3A%2F%2Fhackmd.io%2F%40Mes%2FPreface)
+ [語言技術:C++ Gossip](https://openhome.cc/Gossip/CppGossip/)
+ [菜鸟教程](https://www.runoob.com/)
### 其他參考資料
- [lambda 運算式](https://openhome.cc/Gossip/CppGossip/Lambda.html)
- [set](https://web.ntnu.edu.tw/~algo/Set.html)
+ [spanning tree](https://web.ntnu.edu.tw/~algo/SpanningTree.html)
+ [集合符號](https://www.shuxuele.com/sets/symbols.html)
+ [線上程式編輯器](https://www.onlinegdb.com/)
### Word筆記
- 這是在 Hack_MD 分類排版前的截圖,證明這真的是高一、二做的筆記。
[原始連結 : 程式用法.doc](https://docs.google.com/document/d/1Pl1eM9fBid-g8d9Q4_qyhdRgS8OMeHZK-g4m9i4MqH8/edit?usp=sharing)

+ [高二升高三暑假培訓筆記.doc](https://docs.google.com/document/d/11gBtor8vRsMYw_O-fGjuiLnUVB-Qevg-54C9tNCr1UA/edit?usp=sharing)
#### 資培
- [資培營7月](https://drive.google.com/drive/folders/1VbmCskpeMRhsyJxd6K7gAHXJgP-tT29O)
- [20240804](https://drive.google.com/drive/folders/1ZNEMr3uDsXkotfKnCrnJvvS1H_ia68XR)
### 線上題庫
+ [高中生程式解題系統 ZERO judge](https://zerojudge.tw/)
+ [Codeforces](https://codeforces.com/)
+ [CITRCjudge](https://judge.citrc.tw/problem)
由彰中學生購買伺服器自行架設的解題系統
**我有出一份力 (錢)**