[英]C++: convert vector of floats to char*
How can I convert vector of floats into a char*?如何将浮点数向量转换为 char*?
I have a collection of floats stored inside std::vector<float> id_list
:我在
std::vector<float> id_list
存储了一组浮点数:
0,
0,
0,
0,
0,
1.77636e-15,
2.35099e-38,
-7.10543e-15,
3.06107e-38,
....
and using this code to convert it:并使用此代码进行转换:
char *ptr = (char*)&id_list[0];
std::string dump_string(ptr, id_list.size() * sizeof(float));
but dump_string.c_str()
returns empty, despite having some values stored in my vector.但是
dump_string.c_str()
返回空,尽管我的向量中存储了一些值。 I'm expecting to get all values concatenated into a one, long string, ie.:我期望将所有值连接成一个长字符串,即:
0,0,0,0,0,1.77636e-15,2.35099e-38,-7.10543e-15,3.06107e-38........
I'm expecting to get all values concatenated into a one, long string, ie.:
我期望将所有值连接成一个长字符串,即:
0,0,0,0,0,1.77636e-15,2.35099e-38,-7.10543e-15,3.06107e-38........
0,0,0,0,0,1.77636e-15,2.35099e-38,-7.10543e-15,3.06107e-38........
You are creating a std::string
that simply holds a copy of the raw bytes of the float
array.您正在创建一个
std::string
,它只保存float
组的原始字节的副本。 So, if any of the bytes happen to contain a numeric value of 0 (as is the case in your example), you will get a truncated result if you try to treat the std::string
as a null-terminated C string .因此,如果任何字节碰巧包含 0 的数值(如您的示例中的情况),则如果您尝试将
std::string
视为以空字符结尾的 C string ,则会得到截断的结果。
If you want to actually convert the float values into a human-readable string, you need to format the values individually, such as with std::ostringstream
or std::to_string()
, eg:如果要将浮点值实际转换为人类可读的字符串,则需要单独格式化这些值,例如使用
std::ostringstream
或std::to_string()
,例如:
std::ostringstream oss;
if (!id_list.empty()) {
oss << id_list[0];
for(size_t i = 1; i < id_list.size(); ++i) {
oss << "," << id_list[i];
}
}
std::string dump_string = oss.str();
std::string dump_string;
if (!id_list.empty()) {
dump_string += std::to_string(id_list[0]);
for(size_t i = 1; i < id_list.size(); ++i) {
dump_string += ',';
dump_string += std::to_string(id_list[i]);
}
}
.
. I'm expecting to get all values concatenated into a one, long string, ie.:
我期望将所有值连接成一个长字符串,即:
You could write a small function to do that.你可以写一个小函数来做到这一点。
Example:例子:
#include <iostream>
#include <iterator> // std::advance
#include <string>
#include <sstream> // std::stringstream
#include <vector>
// a function taking two iterators and streaming the content to a stringstream
// then returning a `std::string` using `stringstream::str()`
template<typename Begin, typename End = Begin>
std::string join(Begin begin, End end) {
std::stringstream retval;
if(begin != end) {
retval << *begin;
for(std::advance(begin, 1); begin != end; std::advance(begin, 1)) {
retval << ',' << *begin;
}
}
return retval.str();
}
int main()
{
std::vector<float> id_list {
0,
0,
0,
0,
0,
1.77636e-15,
2.35099e-38,
-7.10543e-15,
3.06107e-38,
};
std::cout << join(id_list.begin(), id_list.end());
}
Output:输出:
0,0,0,0,0,1.77636e-15,2.35099e-38,-7.10543e-15,3.06107e-38
As others note, your approach of using casting is not the right way to go.正如其他人指出的那样,您使用强制转换的方法不是正确的方法。 Also, note that all of the value in your example are probably exactly 0.0 (or -0.0)!
另外,请注意您示例中的所有值可能恰好是 0.0(或 -0.0)! Why?
为什么? Because they're beyond the precision range of
float
on typical platforms.因为它们超出了典型平台上
float
的精度范围。
Still, you could concatenate the string representation of the float
's in an std::vector
, using the ranges library , and with no need for any loops:不过,您可以使用range 库将
float
的字符串表示连接到std::vector
,并且不需要任何循环:
#include <vector>
#include <string>
#include <iostream>
#include <range/v3/all.hpp>
std::string concat_my_floats(const std::vector<float>& vec)
{
std::ostringstream oss;
auto convert = [&](float x) { oss.str(""); oss << x; return oss.str(); };
return vec
| ranges::views::transform(convert)
| ranges::views::cache1 // We need this, see footnote.
| ranges::views::join(',')
| ranges::to<std::string>();
}
int main () {
std::vector<float> id_list = {0, 1.1, -2.2, 3.3};
std::cout << concat_my_floats(id_list) << std::endl;
}
This will give you:这会给你:
0,1.1,-2.2,3.3
If you're wondering what's that cache1
business - it has to do with how the transformed range is a range of prvalues, which the ranges library is not willing to join for performance reasons;如果您想知道
cache1
业务是什么 - 它与转换范围如何成为一个纯右值范围有关,范围库出于性能原因不愿意加入; apparently you need to explicitly allow a caching of the last element, expensive though it may be.显然您需要明确允许缓存最后一个元素,尽管它可能很昂贵。 See here .
见这里。
How can I convert vector of floats into a char*?
如何将浮点数向量转换为 char*?
Here is one way to reinterpret the array of floating point numbers as bytes:这是将浮点数数组重新解释为字节的一种方法:
char *ptr = reinterpret_cast<char*>(id_list.data());
Which does exactly what your cast did except it doesn't rely on a C style cast.除了它不依赖于 C 风格的演员之外,这完全符合您的演员阵容。
but dump_string.c_str() returns empty
但 dump_string.c_str() 返回空
This is because dump_string
happens to contain an empty null terminated string at the beginning.这是因为
dump_string
刚好在开头包含一个空的空终止字符串。
I'm expecting to get all values concatenated into a one, long string, ie.:
我期望将所有值连接成一个长字符串,即:
0,0,0,0,0,1.77636e-15,2.35099e-38,-7.10543e-15,3.06107e-38........
You expectation is misguided.你的期望被误导了。 Reinterpreting bytes of floating point numbers is a separate thing from serialising them to readable text.
重新解释浮点数的字节与将它们序列化为可读文本是不同的。 If this is the output you want , then reinterpretation is not the operation that you need.
如果这是您想要的输出,那么重新解释不是您需要的操作。
You can use for example a string stream to convert floating point numbers to text.例如,您可以使用字符串流将浮点数转换为文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.