資料多的時候可能有點慢,不過很好用
string str =
"123"
;int num;
stringstream ss;
ss << str;
ss >> num;
cout << num << endl;
int num =
32.123
; string str;
stringstream ss;
ss << num;
ss >> str;
//cout << ss.str() endl;一樣的效果
cout << str;
char str=
"123456"
;int num;
sscanf
(str,
"%d"
,&num)
;//這裡切換型別即可
cout << num << endl;
char str[10]
;int num =
123465
;sprintf
(str,
"%d"
,num)
;//想改變其他的只需改變這的型別
cout << str;
注意:在使用的時候這裡也可以直接轉換進製
如:
//例項1
char str=
"171"
;int num;
sscanf
(str,
"%x"
,&num)
; cout << num << endl;
//轉為了16進製制
//例項2
char str=
"ab"
;int num;
sscanf
(str,
"%x"
,&num)
;//16進製制轉為10進製,171
cout << num << endl;
例:char str=
"171"
;int num =
atoi
(str)
; cout << num << endl;
例:
函式原型:char *itoa(int value, char *string, int radix);
value為輸入整數,string為輸出轉換的字串,radix 為以幾進製輸出
int num =23;
char str[10]
;itoa
(num,str,10)
; cout << str;
char p[20]
="hello world!"
; string str
(p);
cout << str;
string str=
"hello world"
;const
char
*c = str.
c_str()
; cout << c << endl;
const
char
* tmp =
"hello world"
;char
* p =
const_cast
<
char
*>
(tmp)
; cout << p << endl;
char
* p =
"hello world"
;const
char
* temp = p;
cout << temp << endl;
數字與字串轉換
題目大意 給定兩個數 i 和 j 將數字 i j 翻轉後按公升序排列輸出。include include include include includeusing namespace std struct node num 55 翻轉字串 char strrev char s int len str...
C 中數字與字串的轉換
1 字串數字之間的轉換 1 string char string str ok char p str.c str 2 char string char p ok string str p 3 char cstring char p ok cstring m str p 或者 cstring m st...
c 數字與字串轉換方法小結
1.sstream轉換 標頭檔案 include 範例 int i 0 string out stringstream ss ss out cout2.to string 標頭檔案 include 範例 int i 0 string str str to string i cout3.stoi 標頭...