#include<iostream>
#include<sstream>
#include<stdlib.h>
using namespace std;
string int2str(int num,string s){
stringstream ss;
ss<<num;
ss>>s;
return s;
}
int main(){
//整数转字符串 ( #include<sstream>)
string str;
int i =1234;
str=int2str(i,str);
cout<<str;
//整数转字符串 ( # include <stdlib.h>)
int num = 100;
char str1[25];
itoa(num, str1, 10);//第三个参数是进制
cout<<str1;
//整数转字符串(最简单)
string s = to_string(a);
//字符串转数字
int figure;
char str2[6]="99990";
figure = atoi(str2); //只能在C中使用 LeetCode C++中是不能使用的
cout<<figure;
return 0;
//字符串转数字
string str3 = "123"
int number = stoi(str3); //最简单
// 利用stoi()将字符串转换为int型数字
string h = "123";
int j = stoi(h);
cout << "利用stoi()将字符串转换为int类型:" << j << endl;
string k = "1.12345678";
double z = stod(k);
cout << "利用stod()将字符串转换为double类型:" << z << endl;
float x = stof(k);
cout << "利用stof()将字符串转换为float类型:" << x << endl;
// 利用stringstream将字符串转换为数字
string str4 = "1.12345678";
double dnumber;
stringstream f;
f << str4;
f >> dnumber;
cout << "字符串转数字:" << dnumber << endl;
}
字符串和数字的相互转换
于 2019-03-02 14:56:24 首次发布