Re: [問題] 陣列內中英文字串的反轉(序)

看板C_and_CPP作者 (小瑜)時間15年前 (2009/03/22 12:15), 編輯推噓5(507)
留言12則, 3人參與, 最新討論串4/4 (看更多)
#include <iostream> #include <string> #include <algorithm> #include <fstream> using namespace std; int main() { ifstream Input("input.txt"); ofstream Output("output.txt"); if(!Input) { exit(1); } string temp; while(getline(Input, temp)) { for(string::size_type ix = 0; ix < temp.size(); ix++) if(temp[ix] < 0) swap(temp[ix], temp[ix++]); reverse(temp.begin(), temp.end()); Output << temp << '\n'; temp.clear(); } Input.close(); Output.close(); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.116.165.235 ※ 編輯: joefaq 來自: 59.116.165.235 (03/22 12:17)

03/22 12:17, , 1F
不過我是試著使用陣列去互相傳入,目前不太考慮用
03/22 12:17, 1F

03/22 12:17, , 2F
reverse來寫,還是說不用reverse會寫不出來 囧?
03/22 12:17, 2F
※ 編輯: joefaq 來自: 59.116.165.235 (03/22 12:21)

03/22 12:40, , 3F
我也先試試看這方法,感謝(跪
03/22 12:40, 3F

03/22 12:43, , 4F
這方法完全可行 囧!我繼續研究(磕頭
03/22 12:43, 4F

03/22 18:58, , 5F
基本上都看懂了,不過我有一個地方不太懂,就是判斷是
03/22 18:58, 5F

03/22 18:58, , 6F
否為中文字的那個swap,為什麼是寫if(temp[ix] < 0)呢
03/22 18:58, 6F

03/22 20:07, , 7F
你用bitwise去想 中文字的特徵就是第一個byte的第一bit是
03/22 20:07, 7F

03/22 20:07, , 8F
零,這對應到signed char型態的話,就是負數了
03/22 20:07, 8F

03/22 20:08, , 9F
更正 第一個byte的第一個bit是1
03/22 20:08, 9F

03/22 21:52, , 10F
感謝O3Q 我懂了!
03/22 21:52, 10F

03/23 18:06, , 11F
想請問一下 我這個方法在DevC可行 可是在VC就沒辦法
03/23 18:06, 11F

03/23 18:06, , 12F
在VC 中文字串會變亂碼 這是為什麼?
03/23 18:06, 12F
文章代碼(AID): #19nRjsrl (C_and_CPP)
文章代碼(AID): #19nRjsrl (C_and_CPP)