首先来看一组测试:
string转int:
int stoi(string str)
{
int result;
istringstream is(str);
is >> result;
return result;
}
string转float:
float stof(string str)
{
float result;
istringstream is(str);
is >> result;
return result;
}
string转double:
double stod(string str)
{
double result;
istringstream is(str);
is >> result;
return result;
}
再来看一组测试:
int转string:
string itos(int i)
{
ostringstream os;
os<<i;
string result;
istringstream is(os.str());
is>>result;
return result;
}
float转string:
string ftos(float f)
{
ostringstream os;
os<<f;
string result;
istringstream is(os.str());
is>>result;
return result;
}
double转string:
string dtos(double d)
{
ostringstream os;
os<<d;
string result;
istringstream is(os.str());
is>>result;
return result;
}
看到上述之后,规律出来了,总结如下:
*转string
string *tos(* i) //改一下函数名,改一下类型,搞定
{
ostringstream os;
os<<i;
string result;
istringstream is(os.str());
is>>result;
return result;
}
string转*
* sto*(string str) //改一下函数名,变量类型,搞定
{
* result;
istringstream is(str);
is >> result;
return result;
}
送一个在线编译C/C++的网站:http://coliru.stacked-crooked.com/
文章的脚注信息由WordPress的wp-posturl插件自动生成

微信扫一扫,打赏作者吧~![[整理][转载]win下网卡抓包发包库Npcap使用](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2023/08/demo_1-1024x711.jpg&w=280&h=210&zc=1)
![[转载]基础数据char,int,double,string是线程安全的吗?](http://www.jyguagua.com/wp-content/themes/begin/img/random/14.jpg)
![[整理]用c++编写的RDTSC性能计时器](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/12/rdtsc-assembly-example.jpg&w=280&h=210&zc=1)
![[整理]strcmp汇编写法](http://www.jyguagua.com/wp-content/themes/begin/img/random/4.jpg)