易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
C++ 简单的字符串转换
C++ 简单的字符串转换:
#include <iostream>
#include <string>
#include <sstream>
#include <complex>
using
namespace
std;
template
<
typename
T>
T fromString(
const
string &s)
{
istringstream is(s);
T t;
is >> t;
return
t;
}
template
<
typename
T>
string toString(
const
T &t)
{
ostringstream s;
s << t;
return
s.str();
}
int
main(
int
argc,
char
*argv[])
{
int
i = 12345;
cout <<
"i == ""
<< toString(i) <<
"""
<< endl;
float
x = 567.89;
cout <<
"x == ""
<< toString(x) <<
"""
<< endl;
complex<
float
> c(1.0, 2.0);
cout <<
"c == ""
<< toString(c) <<
"""
<< endl << endl;
i = fromString<
int
>(string(
"1234"
));
cout <<
"i == "
<< i << endl;
x = fromString<
float
>(string(
"567.89"
));
cout <<
"x == "
<< x << endl;
c = fromString< complex<
float
> >(string(
"(1.0, 2.0)"
));
cout <<
"c == "
<< c << endl;
return
0;
}
运行结果为:
i == "12345"
x == "567.89"
c == "(1,2)"
i == 1234
x == 567.89
c == (1,2)
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图