字符串转数字
//字符串转数字,如果字符串以0开头,则会去掉这个0
#include <iostream>
using namespace std;
#include <sstream>
void s2i(string str, int &n);
int main()
{
string s1 = "01234";
int n1 = 0;
s2i(s1,n1);
cout << n1 << endl;
return 0;
}
void s2i(string str, int &n)
{
stringstream ss;
ss << str;
ss >> n;
}
数字转字符串
#include <iostream>
using namespace std;
#include <sstream>
void i2s(string &str, int n);
int main()
{
string s1;
int n1 = 188;
i2s(s1,n1);
cout << n1 << endl;
return 0;
}
void i2s(string &str, int n)
{
stringstream ss;
ss << n;
ss >> str;
}
这两段代码还是蛮常用的,主要区别是在调用函数时看你返回什么,&就给谁。
这些东西是在准备 蓝桥杯 的过程中学到的,虽然没有拿到奖,但是居然在实习的时候有点小用处,大三代码也基本上是自己写的,还是蛮欣慰的,就是得感慨一句,太费眼睛了。