C++ string 與char互換關係
二月 10, 2025
char
陣列<=>C-style
字符串
char陣列轉string
1 | char arr[] = "Hello"; |
string轉char陣列
1 | std::string str = "Hello"; |
遍歷string
1 | std::string str = "Hello, World!"; |
C-style
字符串函數:
計算長度
1 | const char* str = "Hello"; |
字串轉述字:
1 | string s; |
數字轉字串:
1 | int num = 123; |
判斷char為數字或是英文:
1 | if(ch[i]>='0'&& ch[i]<='9') |
字串反轉:
1 | reverse(ans.begin(),ans.end()); |
(array
也可以反轉)
1 | reverse(arr.begin(), arr.end()); |
char轉int
1 | int intt=charr-'0'; |
查看评论