利用stringstream 转换最省事的,因为stringstream可以将任意类型转换为目标类型:
#include <iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
stringstream ss1;
int a=456;
string b;
ss1<<a;//需要注意这里的方向,其实跟cin和cout一样,以ss为参考,输入向左,输出向右
ss1>>b;
stringstream ss2;
string c="123";
int d;
ss2<<c;
ss2>>d;
cout<<b<<" "<<d<<endl;//456 123
return 0;
}
本文详细介绍了如何利用C++中的stringstream实现字符串与整型、整型与字符串之间的相互转换,通过具体的代码示例展示了stringstream的使用方法,适用于需要在不同数据类型间进行转换的场景。
4707

被折叠的 条评论
为什么被折叠?



