# Python 及 C++ 遍歷字串 > 作者:王一哲 > 日期:2023年7月29日 ## 前言 由於我大約一年前開始寫 APCS、ZeroJudge 的題目,最近又開始寫 LeetCode,我會先用比較熟悉的 Python 解題,確定程式碼的邏輯正確後,再用 C++ 重寫一遍。在重寫的過程中,因為我對於 C++ 的語法不太熟悉,經常需要上網找資料,沒想到現在 C++ 多了很多方便的工具,甚至有些寫法跟 Python 很像。 **遍歷字串**是指依序讀取字串中的字元並做一些處理,例如將每個字元依照 ASCII 編碼加1,再儲存到另一個字串中,以下是 Python 及 C++ 的寫法。 <br /> ## Python 語法 ### 方法1:使用 for 迴圈及索引值 Python 中的字串類似由字元組成的陣列,索引值由 0 開始,可以用索引值取出字串中的內容。方法1是用 for 迴圈,將變數 i 的值依序由 0、1、2、... 代入,直到 **(字串長度 - 1)** 為止,再用 s\[i\] 就可以依序讀取字串中的字元。 ```python= s = "Hello World!" s2 = "" for i in range(len(s)): s2 += chr(ord(s[i])+1) print(s2) ``` ### 方法2:使用 for 迴圈並暫存字元 Python 的 for 迴圈有一種特別的用法,可以由 in 後方的物件依序取出元素,例如第3行,就是將字串 s 中的字元依序取出並暫存於變數 c,變數 c 在離開 for 迴圈時就會消失。 ```python= s = "Hello World!" s2 = "" for c in s: s2 += chr(ord(c)+1) print(s2) ``` 以上兩種寫法印出的字串 s2 內容皆為 **Ifmmp!Xpsme"**。 <br /> ## C++ 語法 ### 方法1:使用 for 迴圈及索引值 如果想要取得字串 s 的長度,可以用 **s.size()**,回傳值的格式為 **size_t**,是沒有正負號的整數。由於字串類似由字元組成的陣列,索引值由 0 開始,可以用索引值取出字串中的內容。方法1是用 for 迴圈,將變數 i 的值依序由 0、1、2、... 代入,若 i 小於字串長度繼續執行迴圈,再用 s\[i\] 就可以依序讀取字串中的字元。 ```cpp= #include <iostream> #include <string> using namespace std; int main() { string s = "Hello World!", s2 = ""; for(size_t i=0; i<s.size(); i++) { s2 += char(s[i]+1); } cout << s2 << endl; return 0; } ``` ### 方法2:使用 for 迴圈及迭代器 iterator C++ 標準樣版函式庫 (Standard Template Library, STL) 提供了迭代器 (iterator),如果要使用字串的迭代器可以寫成 **string::iterator**,甚至可以用 **auto** 由後方的物件自動判斷迭代器的種類,由於迭代器的值是記憶體位置,如果要取出記憶體位置代表的內容需要在前面加上 **星號 \***,如果還要對這個值再做運算,要再加上 **括號 ()**,例如 **(\*it)**。 ```cpp= #include <iostream> #include <string> using namespace std; int main() { string s = "Hello World!", s2 = ""; for(string::iterator it = s.begin(); it != s.end(); it++) { //for(auto it = s.begin(); it != s.end(); it++) { s2 += char((*it)+1); } cout << s2 << endl; return 0; } ``` ### 方法3:使用 for 迴圈並暫存字元 這個方法很像 Python 的語法,**for(auto c : s)** 會將字串 s 的字元依序取出並存到變數 c。 ```cpp= #include <iostream> #include <string> using namespace std; int main() { string s = "Hello World!", s2 = ""; for(auto c : s) { s2 += char(c+1); } cout << s2 << endl; return 0; } ``` 以上三種寫法印出的字串 s2 內容皆為 **Ifmmp!Xpsme"**。 <br /> ## 結語 現在的 C++ 多了很多方便的工具,我之後會在寫一些筆記,才不會過幾個月沒碰就忘光了。 --- ###### tags:`C++`、`Python`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.