首先請出今日主角:stdlib.h
(YYDS)
這個庫包含有隨機數,abs
等許多通用函數,當然也有類型的轉換
下面我們一點點來看如何完成格式轉換
一、string 和 char []
1. string 轉 char []
string
是一個類,而 char []
的變量名本質上是一個地址,咋一看這倆好像不太好轉換。
但是事實上我們正是可以通過地址的方式將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;
// 雖然傳遞的是地址,但是程序會將內容直接復制到 char [] 中,所以此處改變s不影響a
s = "456.456";
cout << a << endl;
return 0;
}
輸出內容:
123.123
123.123
2. char [] 轉 string
代碼:
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[100] = "123.123";
string s = a;
cout << s;
return 0;
}
二、char [] 與數字互轉
1. char [] 轉整型和浮點型
#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()
兩個函數
當然這兩個函數作為標準庫成員,除了可以像上面這段代碼這樣完成強制類型轉換,處理一些特殊情況也是完全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
如果數字較大需要轉 long
或 long long
,則使用的函數為 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. 整型和浮點型 轉char []
#include <stdio.h>
using namespace std;
int main() {
char a[1001];
sprintf(a, "%.10lf", 3.1415926535);
printf("%s", a);
return 0;
}
絕對沒有比這更香的操作了
printf
輸出到終端,sprintf
可以直接輸出到字符串中。如果字符串中有內容會覆蓋寫入,類似于寫文件
另外 to_string()
函數可以勝任這項工作
警告:這個函數沒有測試過比賽是否可用,請謹慎選擇!!
#include <iostream>
using namespace std;
int main() {
string s = to_string(123);
cout << s << endl;
return 0;
}
3. 整型轉 char [] (特殊函數實現)
警告!下面這段代碼只有win能用,比賽都是不行的!!
看代碼:
#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); // 要轉換的數,存放結果的字符串,結果進制數(下同)
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()
反轉一下就有了 itoa() ltoa()
及 lltoa()
, 還是比較好記的。
以 itoa()
為例,他接受三個參數,其中第三個表示輸出字符串中使用的進制。這又可以在進制轉換上幫我們大忙!
#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
int INT = 12;
char s[16] = {0};
itoa(INT, s, 2); // 12轉二進制
cout << s << endl;
itoa(INT, s, 8); // 轉八進制
cout << s << endl;
itoa(INT, s, 16); // 十六進制
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;
}
到此這篇關于C++中字符串與整型及浮點型轉換全攻略的文章就介紹到這了,更多相關C++中字符串與整型及浮點型轉換全內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!