将大小可变的双精度数组转换为wchar\u t 很新的C++,但我不能把我的脑袋绕在这个

将大小可变的双精度数组转换为wchar\u t 很新的C++,但我不能把我的脑袋绕在这个,c++,arrays,type-conversion,C++,Arrays,Type Conversion,我得到这个双精度数组,只想把它变成一个中间有空格的“字符串” 在java中,我只需迭代所有条目和StringBuilder.append(arr[I]).append(“”) 我不知道如何在C++中做这件事,我想出的最好的办法是这个< /P> wchar_t* getStuff(const double *arr, const int arr_size) { std::vector<wchar_t> result(arr_size*2); for( int i = 0; i

我得到这个双精度数组,只想把它变成一个中间有空格的“字符串”

在java中,我只需迭代所有条目和StringBuilder.append(arr[I]).append(“”)

我不知道如何在C++中做这件事,我想出的最好的办法是这个< /P>
wchar_t* getStuff(const double *arr, const int arr_size)
{
  std::vector<wchar_t> result(arr_size*2);

  for( int i = 0; i < arr_size*2; i++)
  {
    if ( i % 2 == 0 )
        result[i] = ?;
    else
        result[i] = L' ';
  }

  return &result[0];
}
wchar\u t*getStuff(常数双*arr,常数整数arr\u大小)
{
标准::矢量结果(arr_大小*2);
对于(int i=0;i
我知道它不编译,并且包含一些非c代码

我在这里有点不知所措,因为我不知道转换的好方法,也不知道这里到底是什么指针,什么是实际值。

您可以使用a来实现这一点

wchar_t* getStuff(const double *arr, const int arr_size)
{
  std::vector<wchar_t> result(arr_size*2);

  for( int i = 0; i < arr_size*2; i++)
  {
    std::wostringstream theStringRepresentation;
    theStringRepresentation << arr[i];
    // use theStringRepresentation.str() in the following code to refer to the 
    // widechar string representation of the double value from arr[i]
  }

  return &result[0];
}
为什么不简单地使用
std::vector
而不是
std::vector

std::wstring getStuff(常量双精度*arr,常量整数arr\u大小){
标准::矢量结果(arr_大小*2);
对于(int i=0;i在代码中,如果代码在字符串中使用数字,则不能满足任何原因。在C++中,不能返回指向局部变量的指针。相反,返回实际容器,在这种情况下,STD::vector,或者更合适的是STD::String或STD::WString。编译器优化使这个EffI生效。cient.是的,我知道局部变量的返回以未定义的行为返回,只是想保持简短。我不知道为什么我必须使用wchar\u t,我正在使用的api要求我使用wchar:D@Nozdrum有许多常用的API需要使用
std::wchar\u t
,首先想到的是XML解析器像Xerces一样,还有
std::towstring
。您不能返回stringrepresentation.str()吗在stringstream中形成格式后?@sj0h不关心实际逻辑。如果您认为您有改进,请编辑…api要求我使用wchar\t,感谢所有答案和对备选方案的考虑:D
return &result[0]; // Don't do this!
std::wstring getStuff(const double *arr, const int arr_size) {
  std::vector<std::wstring> result(arr_size*2);

  for( int i = 0; i < arr_size*2; i++)
  {
    std::wostringstream theStringRepresentation;
    theStringRepresentation << arr[i];
    // use theStringRepresentation.str() in the following code to refer to the 
    // widechar string representation of the double value from arr[i]
  }

  return result[0];
}