C++ - 转换标准::浮点数组的字符串

C++ - Convert std::string of float array

本文关键字:数组 字符串 转换 标准 C++      更新时间:2023-10-16

这个问题是C++的后续问题 - 将浮点数组转换为 std::string。

如何将std::string转换回使用reinterpret_cast转换为字符串的浮点数组。

c_str()方法获取字符串的支持数据指针。然后将其reinterpret_cast回浮点指针。

const float* array_of_floats = reinterpret_cast<const float*>(str.c_str());
int len = str.size() / sizeof(float);

通常,将二进制数据(例如浮点数数组(序列化为string可以工作,但充其量是奇怪的,而且更可能是不明智的。 最好使用std::vector<uint8_t>作为字节数组来保存浮动指针数据,而不是字符串的实例。