2024-04-22 10:26:06
2024-04-22 05:10:16
#include <sstream> //转换所需的头文件
#include <string>
#include <iostream>
using namespace std;
string numToStr(double i); //声明
int strToNum(string s); //声明
///////////数字转字符串函数_定义///////////
string numToStr(double i)
{
stringstream ss;
ss << i;
cout << i;
return ss.str();
}
//////////字符串转换为数字_定义//////////
int strToNum(string s)
{
int num;
stringstream ss(s);
ss >> num;
cout << num;
return num;
}
int main()
{
int number;
string str;
cout << "字符串转数字:" << endl;
cin >> str;
strToNum(str);
cout << "数字转字符串:" << endl;
cin >> number;
numToStr(number);
system("pause");
return 0;
}
2024-04-22 08:24:59
atoi函数要求参数是个const char*,就是常量字符串,string的c_str()方法返回的就是const char*
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s="12345";
int n=atoi(s.c_str());
cout<< n<<endl;
return 0;
}
2024-04-22 00:31:38
2024-04-22 11:02:07