[問題] InttoString 問題

看板C_and_CPP作者 (lantis)時間11年前 (2014/10/06 15:22), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串1/1
開發平台(Platform): Linux ubuntu 問題(Question): code: ----------------- char i_str[256],j_str[256]; sprintf(i_str,"%d",i); sprintf(j_str,"%d",j); str+='('+i_str+','+j_str+','+combine[i]+','+combine[j]+')'+','; cout<<str<<endl; -------------------- 簡單的int to string Error Code:|332|error: invalid operands of types ‘char*’ and ‘char [256]’ to binary ‘operator+’| 關鍵應該是在i_str和j_str i , j ,型態皆為int combine[i],combine[j] 為vector<string> 預期的正確結果(Expected Output): (1,2,source,target), -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.22.21.92 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1412580127.A.43E.html

10/06 16:59, , 1F
那個錯誤訊息說的是你不能用字元去+ 指標(char*)
10/06 16:59, 1F

10/06 17:54, , 2F
指標 + 字元陣列
10/06 17:54, 2F

10/06 17:59, , 3F
不過錯誤是在更前面的地方,用 '('+i_str 就有問題了
10/06 17:59, 3F

10/06 20:03, , 4F
拼接這種東西用stringstream會比一堆+效能好且好看
10/06 20:03, 4F

10/06 21:11, , 5F
你可以使用strcat把char*串起來,就可以達到你要的效果
10/06 21:11, 5F

10/06 21:14, , 6F
然後要不就都使用string配合stringstream
10/06 21:14, 6F

10/06 21:15, , 7F
要不就使用char*配合sprintf,不要混著使用
10/06 21:15, 7F
文章代碼(AID): #1KCaCVG- (C_and_CPP)