C++中字符串與整型及浮點型轉(zhuǎn)換全攻略
首先請出今日主角:stdlib.h
(YYDS)
這個庫包含有隨機(jī)數(shù),abs
等許多通用函數(shù),當(dāng)然也有類型的轉(zhuǎn)換
下面我們一點點來看如何完成格式轉(zhuǎn)換
一、string 和 char []
1. string 轉(zhuǎn) char []
string
是一個類,而 char []
的變量名本質(zhì)上是一個地址,咋一看這倆好像不太好轉(zhuǎn)換。
但是事實上我們正是可以通過地址的方式將string
中的值整體地遷移到 char []
中:
#include <string.h> #include <iostream> using namespace std; int main() { string s = "123.123"; char a[101]; strcpy(a, s.c_str()); // strcpy(a, s.data()); // 與上方語句等價,任選其一即可 cout << a << endl; // 雖然傳遞的是地址,但是程序會將內(nèi)容直接復(fù)制到 char [] 中,所以此處改變s不影響a s = "456.456"; cout << a << endl; return 0; }
輸出內(nèi)容:
123.123
123.123
2. char [] 轉(zhuǎn) string
代碼:
#include <bits/stdc++.h> using namespace std; int main() { char a[100] = "123.123"; string s = a; cout << s; return 0; }
二、char [] 與數(shù)字互轉(zhuǎn)
1. char [] 轉(zhuǎn)整型和浮點型
#include <stdlib.h> #include <iostream> using namespace std; int main() { char a_chars[101] = "123.123"; int a_int = atoi(a_chars); double a_double = atof(a_chars); cout << a_int << endl; cout << a_double << endl; return 0; }
輸出:
123
123.123
用到了頭文件 stdlib.h
中的 atoi()
和 atof()
兩個函數(shù)
當(dāng)然這兩個函數(shù)作為標(biāo)準(zhǔn)庫成員,除了可以像上面這段代碼這樣完成強(qiáng)制類型轉(zhuǎn)換,處理一些特殊情況也是完全OK
#include <stdlib.h> #include <iostream> using namespace std; int main() { char a_chars[101] = "00123"; int a_int = atoi(a_chars); cout << a_int << endl; char b_chars[101] = "-013.470"; double b_double = atof(b_chars); cout << b_double << endl; return 0; }
輸出:
123
-13.47
如果數(shù)字較大需要轉(zhuǎn) long
或 long long
,則使用的函數(shù)為 atol()
及 atoll()
,用法與 atoi()
相同:
#include <stdlib.h> #include <iostream> using namespace std; int main() { char a_chars[101] = "00123"; long a_long = atol(a_chars); // long cout << a_long << endl; long long a_long_long = atoll(a_chars); // long long cout << a_long_long << endl; return 0; }
2. 整型和浮點型 轉(zhuǎn)char []
#include <stdio.h> using namespace std; int main() { char a[1001]; sprintf(a, "%.10lf", 3.1415926535); printf("%s", a); return 0; }
絕對沒有比這更香的操作了
printf
輸出到終端,sprintf
可以直接輸出到字符串中。如果字符串中有內(nèi)容會覆蓋寫入,類似于寫文件
另外 to_string()
函數(shù)可以勝任這項工作
警告:這個函數(shù)沒有測試過比賽是否可用,請謹(jǐn)慎選擇??!
#include <iostream> using namespace std; int main() { string s = to_string(123); cout << s << endl; return 0; }
3. 整型轉(zhuǎn) char [] (特殊函數(shù)實現(xiàn))
警告!下面這段代碼只有win能用,比賽都是不行的?。?br />
看代碼:
#include <stdlib.h> #include <iostream> using namespace std; int main() { int INT = 123; long LONG = 123456; long long LONG_LONG = 123456789; char s[16] = {0}; itoa(INT, s, 10); // 要轉(zhuǎn)換的數(shù),存放結(jié)果的字符串,結(jié)果進(jìn)制數(shù)(下同) cout << s << endl; ltoa(LONG, s, 10); cout << s << endl; lltoa(LONG_LONG, s, 10); // 這里編譯時有warning,原因不詳 cout << s << endl; return 0; }
輸出:
123
123456
123456789
atoi() atol()
及 atoll()
反轉(zhuǎn)一下就有了 itoa() ltoa()
及 lltoa()
, 還是比較好記的。
以 itoa()
為例,他接受三個參數(shù),其中第三個表示輸出字符串中使用的進(jìn)制。這又可以在進(jìn)制轉(zhuǎn)換上幫我們大忙!
#include <stdlib.h> #include <iostream> using namespace std; int main() { int INT = 12; char s[16] = {0}; itoa(INT, s, 2); // 12轉(zhuǎn)二進(jìn)制 cout << s << endl; itoa(INT, s, 8); // 轉(zhuǎn)八進(jìn)制 cout << s << endl; itoa(INT, s, 16); // 十六進(jìn)制 cout << s << endl; return 0; }
輸出:
1100
14
c
再次警告!上面這段代碼只有win能用,比賽都是不行的??!
提一嘴:文中用到了 s.c_str() 的寫法。如果你需要使用 printf() 輸出 string 類型的字符串,也需要這樣:
#include <stdio.h> #include <string.h> using namespace std; int main() { string str = "123"; printf("str:%s", str.c_str()); // printf("str:%s", str); // 這樣寫真的不行 return 0; }
到此這篇關(guān)于C++中字符串與整型及浮點型轉(zhuǎn)換全攻略的文章就介紹到這了,更多相關(guān)C++中字符串與整型及浮點型轉(zhuǎn)換全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Matlab實現(xiàn)數(shù)字音頻分析處理系統(tǒng)
這篇文章主要為大家介紹了如何利用Matlab制作一個帶GUI的數(shù)字音頻分析與處理系統(tǒng)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2022-02-02