# getline 函式 :::warning <font color='blue'> $getline 練習$ </font> 1. 讓使用者輸入一行字串 2. 利用getline(cin, s)輸出輸入資料 3. 延伸問題:輸入多行並輸出 ::: ## 程式碼 ```cpp= #include <iostream> #include <cstring> using namespace std; int main() { // 宣告一個記憶體空間來儲存資料 string s; // 讀取使用者輸入的一行資料 while(getline(cin, s)){ // 輸出資料 cout << s <<"\n"; } return 0; } ``` ### 結果 ![](https://i.imgur.com/WhEvEkv.png) # stringstream類別 :::warning <font color='blue'>$stringstream練習$ </font> 請寫一個程式,當輸入值為一行英文時,請將非英文字母部分轉成空白鍵, 並將所有字母改成小寫,最後依照順序輸出每個單字,一行一個單字。 ::: ## 程式碼 ```cpp= #include <iostream> #include <cctype> // isalpha(), tolower #include <sstream> // stringstream using namespace std; int main() { // 儲存輸入的每一行字串資料 string s; // 不斷的讀取輸入資料直到輸入結束 while(getline(cin, s)){ //scar s for every char for(int i=0; i<s.length(); i++){ //判斷第i個字元是否為英文字元 if(isalpha(s[i])) // 輸入字元是英文字元,全部轉為小寫 s[i]=tolower(s[i]); // 不是英文字元,轉為空白字元 else s[i]=' '; } //cout<<s<<"\n"; // 以 s 建構一個 stringstream物件 stringstream ss(s); // 暫存字串變數 string tmp; // 將 ss串流以空白分割的子字串存入temp while(ss>>tmp)cout<<tmp<<"\n"; } return 0; } ``` ### 結果 ![](https://i.imgur.com/6AhpVOs.png) # 大量修改與比對資料 :::info 1. 若要大量修改與比較資料,可使用函式memset、memcpy 與 memcmp 2. 相較於使用迴圈進行資料的修改,有較佳的執行速度,但須小心使用,要注意記憶體空間是否超出範圍。 ::: :::success memset(): 大區域記憶體空間值設定 1. 區段有可能在存取過程中超出記憶體範圍,會產生區段錯誤(Segmentation fault),造成程式執行中斷 ::: ## 錯誤寫法導致系統無法分配記憶體,須將區域變數改為全域變數 ```cpp= #include <iostream> #include<cstring> // for memset #define SIZE 10000000 using namespace std; int main() { int a[SIZE]; return 0; } ``` <font color='Orange'>沒有returned 0 代表錯誤 </font> ![](https://i.imgur.com/Dk75Wqn.png) ![](https://i.imgur.com/Hd8z7rP.png) ### 解決方法 ```cpp= #include <iostream> #include<cstring> // for memset #define SIZE 10000000 using namespace std; int a[SIZE]; int main() { return 0; } ``` <font color='Orange'>returned 0 代表正確 </font> ![](https://i.imgur.com/5WpD3KD.png) ::: warning <font color='blue'>$memset練習$ </font> 1. 練習 memset()的使用 2. 常數定義方式 3. 觀察 宣告後 未給初值 的記憶體內容,以及給定設定值的記憶體內容 4. 輸出每10個元素為一行,且倆倆之間以一個空白隔開 ::: ```cpp= #include <iostream> #include<cstring> // for memset // #define SIZE 10000000 const int SIZE2 = 100; using namespace std; int a[SIZE]; int b[SIZE2]; void showData() { bool first = true; for(int i=0; i<SIZE2; i++){ if(first){ cout<<b[i]; first=false; }else{ cout<<" "<<b[i]; } if((i+1)%10==0){ cout<<"\n"; first=true; } } } int main() { memset(b, 0, sizeof(b)); showData(); return 0; } ``` ![](https://i.imgur.com/6jb9eaT.png) # 位元運算 :::info 1. 自己跟自己 做$XOR$運算結果為 $0$ 2. 自己跟 $0$ 做運算結果為 $自己$ 3. 自己跟 $1$ 做運算結果為 $反向自己$ ::: ## Data type練習 ```cpp= #include <iostream> using namespace std; // 在程式中以 UINT32 代表 unsigned int typedef unsigned int UINT32; // 自訂線段資料型態結構 typedef struct _line{ //線段起點 int start; //線段終點 int end; }LINE; //struct _line 以 LINE 為別名 typedef struct _students{ //學生的名字 string name; //身高 double height; //提重 double weight; //BMI值 double BMI; bool gender; }STUDENT; // struct _student 以 STUDENT 為別名 //宣告儲存 30個學生的陣列 STUDENT students[30]; int main() { for(int i=0; i<30; i++){ students[i].name = "ST" ; students[i].height = (i+i)*1.05; students[i].weight = 50*1.05; if(!(i%2)) students[i].gender=1; else students[i].gender=0; } for(int i=0; i<30; i++){ cout<<students[i].name<<" " <<students[i].height<<" " <<students[i].weight<<" " <<students[i].gender<<"\n "; } return 0; } ``` ![](https://i.imgur.com/Ze2nIEq.png)