C++ 将C+中的double*转换为std::string+;

C++ 将C+中的double*转换为std::string+;,c++,string,double,C++,String,Double,我想在C++中将double*转换为string: #include <iostream> #include <string> using namespace std; int main() { int i; double *f = new double[5]; for(i = 0; i < 5; i++) { f[i] = 5; } string f_str; //this is for double to str

我想在C++中将
double*
转换为
string

#include <iostream>
#include <string>

using namespace std;

int main()
{
  int i;
  double *f = new double[5];

  for(i = 0; i < 5; i++)
  {  
    f[i] = 5;
  }

  string f_str;

  //this is for double to string
  //f_str = std::to_string(f);
  //i want for double*

  cout << f_str << '\n';

  delete [] f;

  return 1;
}
#包括
#包括
使用名称空间std;
int main()
{
int i;
double*f=新的double[5];
对于(i=0;i<5;i++)
{  
f[i]=5;
}
字符串f_str;
//这是双人串
//f_str=std::to_字符串(f);
//我要双份的*
不能尝试使用:

std::stringstream-ss;
对于(i=0;i<5;i++)
{  
f[i]=5;

ss您可以尝试以下代码。希望这对您有所帮助,老兄。谢谢

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

#define MAX 1000

using namespace std;
typedef long long ll;

string DOUBLE_TO_STRING(double data)
{
    string number;

    stringstream out ;
    out << data;
    number = out.str();

    return(number);

}


int main()
{

    ll n;

    while(cin >> n)
    {
        double a[MAX];
        vector<string> str;

        for(ll i=0; i<n; i++){
            cin >> a[i];
            str.push_back(DOUBLE_TO_STRING(a[i]));
        }

        for(ll i=0; i<str.size(); i++)
            cout << str[i] << "\n";
    }


    return 0;
}
#包括
#包括
#包括
#包括
#定义最大1000
使用名称空间std;
typedef long-long-ll;
字符串双精度到双精度字符串(双精度数据)
{
字符串编号;
流出来;
输出>n)
{
双a[最大值];
载体str;
对于(ll i=0;i>a[i];
str.push_-back(双_-TO_-STRING(a[i]));
}
对于(ll i=0;i试试这个

#include <iostream>
#include <string>
#include <sstream>

int main( )
{
    const int SIZE(5);
    double *f = new double[SIZE];

    // fill data
    for(int i(0); i < SIZE; i++)
        f[i] = 5;

    std::string doubArray2Str;
    for(int i(0); i < SIZE; ++i){
        std::ostringstream doubleStr;
        if ( i == SIZE - 1 )
          doubleStr << f[i]; 
        else
            doubleStr << f[i] << ","; 

        doubArray2Str += doubleStr.str();
    }
    std::cout << doubArray2Str << std::endl;

    delete [] f;
    return 0;
}
#包括
#包括
#包括
int main()
{
常量整数大小(5);
double*f=新的double[尺寸];
//填充数据
for(int i(0);idoubleStr您想输出指针的地址值吗?预期的输出是什么?为什么从程序返回1?不要担心返回1..这是一个例子..我想让我的程序得到一个double*={5.0,5.0,5.0,5.0,5.0,5.0}将其转换为字符串={5.0,5.0,5.0}(如果可能)将它传递给一个只能获取字符串作为参数的函数,然后我将再次将其转换为double*您想转换值而不是指针,对吗?f_str将只保存最后的5.0,我想将它们全部保存在一个字符串中,如果我想再次将f_str转换为double*我将如何做?看一看:如果
double
是流式传输的转换为“string”之后,为什么不直接流式处理double?,因为我只能使用获取字符串参数的函数(我想将其传递给函数)
#include <iostream>
#include <string>
#include <sstream>

int main( )
{
    const int SIZE(5);
    double *f = new double[SIZE];

    // fill data
    for(int i(0); i < SIZE; i++)
        f[i] = 5;

    std::string doubArray2Str;
    for(int i(0); i < SIZE; ++i){
        std::ostringstream doubleStr;
        if ( i == SIZE - 1 )
          doubleStr << f[i]; 
        else
            doubleStr << f[i] << ","; 

        doubArray2Str += doubleStr.str();
    }
    std::cout << doubArray2Str << std::endl;

    delete [] f;
    return 0;
}