[問題] 如何把string的某兩個字元 轉成int

看板C_and_CPP作者 (Mx. YAN)時間11年前 (2014/03/05 17:18), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 問題(Question): 如題 餵入的資料(Input): "02" 程式碼(Code):(請善用置底文網頁, 記得排版) (精簡版) #include <string> int main() { string s = "/0"; int n = 0; cin >> s; . . cout << n + 2; } 補充說明(Supplement): 點點的部分是轉換方法 不會寫 轉完後值存入n 加2後印出 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.17.88

03/05 17:25, , 1F
std::stoi() 不能用不要找我
03/05 17:25, 1F

03/05 20:15, , 2F
stringstream
03/05 20:15, 2F
再問一個問題 假設輸入改為02/01 這樣這不是不能用stringstream? ※ 編輯: nature23306 來自: 140.137.41.50 (03/06 10:03)

03/06 11:10, , 3F
int n1, n2; char c; stringstream ss(s);
03/06 11:10, 3F

03/06 11:10, , 4F
ss >> n1 >> c >> n2;
03/06 11:10, 4F
文章代碼(AID): #1J5klt1W (C_and_CPP)