# 書寫風格
## Header, namespace
:::warning
為了方便閱讀,每個程式碼開頭:
```cpp=
#include <bits/stdc++.h>
using namespace std;
```
請讀者自行斟酌使用。
:::
- `#include <bits/stdc++.h>` 包含了絕大多數競技程式會用到的 header ,簡稱**萬用標頭檔**
- 這樣你不用手打好多個 header
- `using namespace std;` 則表示省略書寫**所有** `std::`
- 如果覺得只有特定幾個需要省略:`using std::XXXX`
## Bracket
為了方便閱讀,在只有一個敘述的循環,不使用大括號。如下
```cpp=
for (int i = L - 1; i > 0; i--)
for (int j = 0; j < i; j++)
if (train[j] > train[j + 1])
{
swap(train[j], train[j + 1]);
cnt++;
}
```
意思等同於:
```cpp=
for (int i = L - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (train[j] > train[j + 1])
{
swap(train[j], train[j + 1]);
cnt++;
}
}
}
```
## 解除同步流、 I/O 綁定
:::warning
以下操作會有效加快在刷題網站的輸入輸出效率,但是不建議與 `printf, scanf` 等混用
```cpp=
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
```
更多內容:[c++ - Significance of ios_base::sync_with_stdio(false); cin.tie(NULL); - Stack Overflow](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull)
:::
## 其他
- `'\n'` 取代 `std::endl`
- 變數命名傾向 snake_case
- `bool` 變數命名: `is_XXXX`
# 常用 STL 標準函式庫
- **二級標題**表示 header
- **一級清單**表示 `std::`
- **二級清單**表示 成員函數,==成員函數有些會重複包含在其他 header==
以下看到的英文,如果都能背起來、知道如何用、且妥善使用,對解題目會有大幅幫助。
:::info
從 C 繼承的 header,會改名如下
```cpp
// C 的函式庫
#include <stdio.h>
#include <string.h>
// 轉換成 C++ 版本
#include <cstdio>
#include <cstring>
```
而在 C++ 版本中,這些 header 也屬於 `std::` 命名空間,比如說
```cpp
printf() // 在 <stdio.h>, <cstdio> 下可以使用
std::printf() // 只有在 <cstdio> 下可以使用
```
然而,混用 printf, scanf, cin, cout 是挺危險的。筆者選擇用 iostream
:::
:::success
C++ 標準函式庫中,某些 header 會隱含包含其他 header,這要看實際 compiler 怎麼定義。
比如說:
- `iostream` 可能包含 `string` `cstdlib`
- `map` 可能包含 `utility`
- `algorithm` 可能包含 `iterator`, `functional`
詳細請看:[cppreference.com](https://en.cppreference.com/index.html)
:::
## iostream
- cin
- ignore()
- cout
- endl
## iomanip
- setprecision()
- fixed
- setw()
- setfill()
- right, left, internal()
## string
- std::to_string()
- string
- empty
- size(), length()
- substr()
- find()
- compare()
- to
- getline()
## sstream
- stringstream
- str()
- clear()
- operator<<, operator>>
- istringstream
- ostringstream
## vector
- vector
- size()
- empty()
- begin(), end()
- push_back(), pop_back()
- front(), back()
- operator[]
- clear()
## algorithm
- sort()
- reverse()
- find()
- max(), min()
- swap()
- next_permutation()
- binary_search()
- lower_bound(), upper_bound()
## map
- map
- insert()
- find()
- count()
- operator[]
- erase()
## unordered_map
- unordered_map
- insert()
- find()
- count()
- operator[]
- erase()
## set
- set
- insert()
- find()
- count()
- erase()
## queue
- queue
- push(), pop()
- front(), back()
- empty()
## stack
- stack
- push(), pop()
- top()
- empty()
## utility
- pair
- first, second
- make_pair()
## tuple
- tuple
- get()
- make_tuple()
- tie()
## bitset
- bitset
- set(), reset()
- flip()
- test()
- count()
- any(), none(), all()
- to_string(), to_ulong()
- operator[]
## cmath
繼承 C 語言
- abs()
- sqrt()
- pow()
- floor(), ceil()
- sin(), cos(), tan()
## cctype
繼承 C 語言
- isalpha(), isdigit()
- tolower(), toupper()
## climits
## cstring
繼承 C 語言
- strlen()
- strcmp()
- strcpy()
## numeric
- accumulate()
- gcd(), lcm() // C++17
- iota()
# C++ 語法
## auto
[C++ auto& vs auto - Stack Overflow](https://stackoverflow.com/questions/29859796/c-auto-vs-auto)
