简体   繁体   English

C ++将2d浮点数数组放入char *

[英]C++ putting a 2d array of floats into a char*

I'm trying to take a 2d vector of floats (input) and put them into a char* (output) in c++. 我正在尝试采用浮点数(输入)的二维向量,并将它们放入c ++中的char *(输出)中。

void foo(const std::vector<std::vector<float> > &input, char* &output )
{  
   char charBuf[sizeof(output)];
   int counter = 0;
   for(unsigned int i=0; i<input.size(); i++)
   {
      for(unsigned int p=0; p<input.at(i).size(); p++)
      {
         //what the heck goes here
      }
   }

You could use a std::stringstream at each iteration to place the float into a std::string and then get the char* array from that. 您可以在每次迭代中使用std::stringstream将浮点数放入std::string ,然后从中获取char*数组。 If you want one big char* array then just use a single string stream defined outside the loops. 如果要一个大char*数组,则只使用循环外定义的单个字符串流。

这很大程度上取决于您“将它们放入char *”的含义。

If you want the result to be human-readable, then you're after snprintf() -- that will convert the number 3.1415 into the string "3.1415". 如果您希望结果易于理解,那么您可以使用snprintf() ,它将数字3.1415转换为字符串“ 3.1415”。 If you're just outputting data to be re-read by another program, then you can just dump the binary data into your output stream. 如果您只是输出要由另一个程序重新读取的数据,则可以将二进制数据转储到输出流中。

Just bear in mind that when you output the binary representation of a floating-point number, you have to be aware of how it's encoded. 请记住,当您输出浮点数的二进制表示形式时,您必须了解其编码方式。 If you're both reading and writing on the same platform, then the encoding will be the same. 如果您在同一平台上进行读取和写入,则编码将相同。 In fact most desktop platforms use use the IEEE-754 spec for floating point numbers, but check up on it just to be safe. 实际上,大多数台式机平台都使用IEEE-754规范来计算浮点数,但是为了安全起见对其进行检查。

Also, make sure that your output stream allows for some sort of versioning or other mechanism for modifying the format later on when you decide to do something different with it. 另外,请确保您的输出流允许某种形式的版本控制或其他机制,以便稍后您决定对其进行其他操作时修改格式。 Just a version number at the beginning is usually sufficient. 通常只需开头的版本号就足够了。

What you're trying to do is called serialization. 您尝试做的事情称为序列化。 The C++ faq has a section dedicated to this: http://www.parashift.com/c++-faq-lite/serialization.html C ++常见问题解答专门针对此部分: http : //www.parashift.com/c++-faq-lite/serialization.html

The following code will get you a char* view of all the float s in your input parameter. 以下代码将为您提供input参数中所有floatchar*视图。

However, I'd very careful that this is actually what you want. 但是,我非常小心,这实际上是您想要的。 The char* array is not endian robust, and I always try to avoid handing allocated pointers back to users. char *数组不是字节序可靠的,并且我始终尝试避免将分配的指针交还给用户。 Anyone using this function will need to deallocate output with delete[] , but that is in no way obvious from the function name or signature, a recipe for fragile code. 使用此函数的任何人都需要使用delete[]释放output ,但是从函数名称或签名(这是易碎代码的秘诀)来看,这绝不是显而易见的。

void foo(const std::vector<std::vector<float> > &input, char* &output )
{  
   //this was likely an error, it is always an array of size 4
   //char charBuf[sizeof(output)];

   std::vector<float> tmp_output;

   int counter = 0;  // Why was this here?

   for(unsigned int i=0; i<input.size(); i++)
   {
      // This is slightly more efficient than the hand rolled loop below.
      // std::copy( input[i].begin(), input[i].end(),
      //            std::back_inserter<float>(tmp_output) );

      for(unsigned int p=0; p<input.at(i).size(); p++)
      {
         tmp_output.push_back(input.at(i).at(p));
      }
   }
   output = new char[tmp_output.size()*sizeof(float)];
   std::copy( reinterpret_cast<const char*>(&(*tmp_output.begin())),
              reinterpret_cast<const char*>(&(*tmp_output.end())),
              output );
}

Don't try to put data into raw character arrays, especially when you don't know their size - which you don't here. 不要尝试将数据放入原始字符数组,尤其是当您不知道它们的大小时(尤其是在此处不知道的地方)。 Use a std::string instead, and a std::stringstream to write to it. 请改用std::stringstd::stringstream进行写入。

Whatever you do, get rid of that charBuf thing; 无论您做什么,都摆脱charBuf事情; anything you do with it will be wrong, since sizeof(output) is not the size of the output array. 您执行的任何操作都是错误的,因为sizeof(output)不是输出数组的大小。

Assuming you want a human-readable string, you might want something like 假设您想要一个易于理解的字符串,则可能需要类似

std::string foo(const std::vector<std::vector<float> > &input)
{
    std::ostringstream stream;
    for (std::size_t i = 0; i < input.size(); ++i)
    {
        if (i != 0) stream << "\n";
        for (std::size_t j = 0; j < input[i].size(); ++j)
        {
            if (j != 0) stream << ",";
            stream << input[i][j];
        }
    }

    return stream.str();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM