# 標準程式庫標頭檔
標頭檔就像一部字典,引入不同的函式庫,賦予意義給其所包含的函式,就像翻開了不同功能的字典。
```cpp=
#include<iostream>
```
`iostream`是其中一個。
# 命名空間
命名空間像是一個識別編號,不同功能的函式可能會有相同的名稱,所以我們需要命名空間來讓程式能精確定義該函式所套用的定義。
```cpp=
#include<iostream>
using namespace std;
```
因為`C++`標準庫就是定義在`std`之下,因此可以在程式一開始加上`using namespace std;`,就是代表接下來的程式碼都是用`std`這個命名空間。
:::info
:::spoiler 不使用`using namespace std;`。
前面提到`C++`標準庫就是定義在`std`之下的,所以要讓電腦成功辨識也可以這樣寫。
```cpp=
#include<iostream>
int main(){
std::cout << "Hello, world" << std::endl;
return 0;
}
```
`cout`這個函式是在標準庫之中的程式碼,所以我們需要`std::`來標記其在`std`這個命名空間底下。
:::
# 主程式(主函式)
每個C++程式都會有一個以上的函式,所以需要一個叫`main`的主程式來執行。
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Hello, world" << endl;
return 0;
}
```
# 輸出&輸入
:::warning
`cout`(輸出)和`cin`(輸入)都是被定義在`std`命名空間和`iostream`函式庫,所以必須要引入該函式空間和命名空間才能使用。
:::
## 輸出
`" "`用來包字串、`' '`用來包字元。(ex. `"string"` 、`'c'`)
:::info
:::spoiler `" "` 和 `' '`的區別。
每個英文字母和數字都佔 1 byte,而中文字占 2 或 3 byte,所以中文要用 `" "` 括起來。
:::
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Hello, world";
}
```
輸出 `Hello, world`。
### 輸出換行
`endl` (end of line) 可以讓輸出換行,`'\n'`也一樣,但的運行速度比較快。
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Hello, world" << endl;
cout << "Hello, world" << '\n';
cout << "Hello, world\n";
}
```
輸出三行 `Hello, world`。
### 輸出運算式子
```cpp=
#include <iostream>
using namespace std;
int main(){
cout << 10 + 3 << '\n';
}
```
輸出 13 。
### 輸出變數
```cpp=
#include <iostream>
using namespace std;
int main(){
int n = 13; //宣告一個名稱為n,值為13的int變數。(之後會介紹)
cout << n;
}
```
輸出`n`的值,也就是 13 。
## 輸入
```cpp=
#include<iostream>
using namespace std;
int main(){
int n; //宣告一個名稱為n。(之後會介紹)
cin >> n;
}
```
```cpp=
#include<iostream>
using namespace std;
int main(){
int n, m; //宣告一個名稱為n、m。(之後會介紹)
cin >> n >> m;
}
```
# 程式終止
`system('pause')`功用主要在執行檔(exe)較為顯著,當程式讀到`system('pause')`時程式會強制暫停。
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Hello, world" <<'\n';
system('pause');
}
```
輸出 `Hello, world`。
<font color="#EB6E41">在`DDJ`解題時不要寫`system('pause')`,系統會出錯。</font>
# 回傳值
`return 0`是代表主函式結束,在編譯器中如果沒有`return 0`也會自動結束程式(包含`DDJ`),但最好還是養成習慣寫,有些如果沒寫會無法編譯。
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Hello, world" <<'\n';
return 0;
}
```
輸出 `Hello, world`。
`return 0`時程式就整個結束了,因此下面打的任何東西都不會執行。
# 例題
###### 有什麼題目沒提到或有新增的煩請留言告知。
[a033: hello, world](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a033)
[a160: 0920初階班作業](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a160)
[a217: 騎職,我拜託你了!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a217)
[a221: 懶貓生日快樂](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a221)
[a660: 春香信仰](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a660)
[a796: 人間不過是你無心的夢、偶然留下的夢 塵世夢](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a796)
[a901: JoGuessr](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a901) (去看[三分搜](https://shawnliang.wiki/post/ternary-search/)的運作方式再寫,簡易演算法。)
[a978: 強大的Coder需要強大的密碼](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a978)
[b154: 商高定理好好玩(easy version)](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=b154)
###### 都看到這了難道不按個愛心支持一下嗎?