/*和c的转换函数比起来用法更为隐蔽,对初学者来说不够直观。*/
#include "iostream"
#include "sstream"
#include "string"
#include "cstdlib"
using namespace std;
int main(void)
{
/*以下是内置类型向string转换的解决方案*/
int ival;
char cval;
ostringstream out_string;
string str;
ival = 100;
cval = 'w';
out_string << ival << " " << cval;
str = out_string.str();
cout << str << endl;
/*以下是string向内置类型转换的解决方案*/
int itmpe;
char ctmpe;
str = "100k";
istringstream in_string( str );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
system( "PAUSE" );
return 0;
}

博客给出了C++中内置类型与string相互转换的解决方案。通过ostringstream实现内置类型向string转换,用istringstream实现string向内置类型转换,并给出了具体代码示例,还包含了输出和暂停程序的操作。
&spm=1001.2101.3001.5002&articleId=79520&d=1&t=3&u=35838f467af44f8f9d1edbca20790591)
692

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



