#include<sstream>
#include<iostream>
using namespace std;
int main()
{
int n = 123456;
char p[100] = {};
stringstream s;
s << n;
s >> p;
cout << p[2];
system("pause");
return 0;
#include<iostream>
using namespace std;
int main()
{
int n = 123456;
char p[100] = {};
stringstream s;
s << n;
s >> p;
cout << p[2];
system("pause");
return 0;
}
C++里stringstream 不能再好用,为啥要用那么差的itoa呢,牛客网还过不了
重复利用stringstream对象
如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;
在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
用sprintf也行啊
#include <stdio.h>
#include <string.h>
int main()
{
int num = -150;
char str[128];
int rsl;
// 数字转字符串
sprintf(str, "%d", num);
// 字符串转数字
sscanf(str, "%d", &rsl);
printf("%s\n", str);
printf("%d\n", rsl);
return 0;
}
别用别用itoa