软件编程
位置:首页>> 软件编程>> C语言>> C++如何通过ostringstream实现任意类型转string

C++如何通过ostringstream实现任意类型转string

  发布时间:2022-06-27 16:23:06 

标签:ostringstream,string

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法:


#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
 int a = 55;
 double b = 65.123;
 string str = "";

 //头文件是sstream
 ostringstream oss;
 oss << a << "---" << b;

 str = oss.str();
 cout << str << endl;
 return 0;
}

输出就是55—65.123,怎么样,转换起来非常的自由。就和输出到屏幕一样。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com