c++中,如何把long 转换成 string??

来源:百度知道 编辑:UC知道 时间:2024/05/23 03:24:02

用库函数 atol()
#include<math.h>
#include <stdlib.h>
long atol(char *s);
注:
turbo C 2.0 函数库。

使用stringstream会方便一点。

#include <stdio.h>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
stringstream stream;
string result;
long n=11111;
stream << n; //从long型数据输入
stream >> result; //转换为 string

cout<<result<<endl; //输出转换后的string
return 0;
}

stringstream是好方法。不过c++里包含iostream头就行了,这里stdio.h没用

2楼是正解

itoa