# C++ 實作 Scanner - 判斷ID、Int、Float ###### tags: `編譯器` `hw1_加分項` `C++` ## 完整程式碼 github: https://github.com/b0989596914/Scanner-like-lex.git ## namespace 在C++的標準函式庫中,所有識別符號都被定義於一個名為std的namespace。 ```cpp= using namespace std; ``` :::info 一個重要的小知識:現在的C++標頭檔為了與C標頭檔區分,已經不支援.h字尾的標頭檔了。 **早期**的C++的<iostream.h> == 在c中呼叫函式(全域性名稱空間) **現在**的<iostream>沒有定義全域性名稱空間,所以必須使用namespace std,才可以讓我們直接使用cout。 ::: ## auto 自動變數類型 ``` auto x = 1; // 等同於 int x = 1; ``` 也可以用在double...等 :::warning 1. 但如果沒有在宣告的時候賦值,像  就會出事 2. 回傳值也不可以用auto類型去宣告,因為要與實際回傳的型別對應 ::: ```cpp= for (auto strIter = str.begin()+add; strIter != str.end(); strIter++) { if (!(isdigit(*strIter))){ return false; } } ``` ## 檔案的讀寫 ### 輸入流與輸出流 在C++當中,只要跟input、output有關的都會產生一個流(stream) :::info **`#include <iostream> `** 全名為input/output stream --> 控制cin、cout **`#include <fstream> `** 全名為file stream --> 控制input file、 output file ::: 但它的使用方始與cin不同 ```cpp= int main(){ fstream fin; fin.open(filePath, ios::in); //開檔 while (getline(fin, str_temp)) { //再逐行讀取 } } ```
×
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