std::向量和双* - 这有多安全?

发布于 2024-09-08 14:46:24 字数 464 浏览 1 评论 0原文

这样做安全吗?

double darray[10];
vector<float> fvector;
fvector.insert(fvector.begin(), darray, darray + 10);  // double to float conversion

// now work with fvector

VS2008 给我一个关于双精度到浮点转换的警告。我该如何摆脱这个警告?我认为将 darray 转换为 float* 是没有意义的,因为这会改变指针的步长(步幅)。

更新:我知道警告表示什么。但与“afloat = adouble;”不同在我可以轻松应用强制转换的情况下,我无法消除这种情况下的警告。

编辑:我编辑了代码,使 darray 不再是函数参数。感谢所有指出这一点的人。

Is it safe to do this?

double darray[10];
vector<float> fvector;
fvector.insert(fvector.begin(), darray, darray + 10);  // double to float conversion

// now work with fvector

VS2008 gives me a warning about the double to float conversion. How do I get rid of this warning? I don't think it makes sense to cast darray to float* as that would change the step size (stride) of the pointer.

Update: I know what the warning indicates. But unlike the "afloat = adouble;" scenario where I can easily apply a cast, I am unable to eliminate the warning in this case.

Edit: I've edited the code so that darray is no longer a function argument. Thanks to all those of you who pointed it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

段念尘 2024-09-15 14:46:24

使用 std::transform() 这允许您提供转换方法。
那么你只需要一个不会生成警告的转换方法:

#include <vector>
#include <algorithm>
#include <iterator>

struct CastToFloat
{
    float operator()(double value) const { return static_cast<float>(value);}
};

int main()
{
    double  data[]  = { 1, 2,3,4,5,6,7,8,9,10};
    std::vector<float>  fl;


    std::transform(data, data+10,
                    std::back_inserter(fl),
                    CastToFloat());
}

Use std::transform() this allows you to provide a conversion method.
Then you just need a conversion method that does not generate a warning:

#include <vector>
#include <algorithm>
#include <iterator>

struct CastToFloat
{
    float operator()(double value) const { return static_cast<float>(value);}
};

int main()
{
    double  data[]  = { 1, 2,3,4,5,6,7,8,9,10};
    std::vector<float>  fl;


    std::transform(data, data+10,
                    std::back_inserter(fl),
                    CastToFloat());
}
终弃我 2024-09-15 14:46:24

您收到警告是因为在 doublefloat 转换中丢失了精度,仅此而已。假设您确实需要 fvector 成为 vector 而不是 vector,很明显您可以接受这种精度损失,所以警告并不重要。

You get the warning because you are losing precision in the double to float conversion, that's all. Assuming that you really need fvector to be a vector<float> and not a vector<double>, it is clear that you can live with this precision loss, so the warning is not important.

谎言 2024-09-15 14:46:24

该警告是关于在没有显式转换的情况下丢失有效数字的。您应该收到与

double d = 1.0;
float f = d;

您可以禁用分配警告相同的警告(请参阅 MSDN 中的#pragma warning)。

The warning is about loss of significant digits without an explicit cast. You should get the same warning for

double d = 1.0;
float f = d;

You can disable the warning for the assignment (see #pragma warning in MSDN).

橘味果▽酱 2024-09-15 14:46:24

VS2008 给我一个关于双精度到浮点转换的警告。我该如何摆脱这个警告?

void some_function(double *darray){
    vector<double> fvector;
    fvector.insert(fvector.begin(), darray, darray + 10);
}

或者

void some_function(float *darray){
    vector<float> fvector;
    fvector.insert(fvector.begin(), darray, darray + 10);
}

或者使用两种变体重载您的函数,或者将其设为模板

template<typename RandomAccessIterator>
void some_function(RandomAccessIterator it){
    typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
    vector<value_type> fvector(it, it + 10);
}

当然,假设固定的 10 并不是一个好主意。请注意,您的原始代码都不安全,因为函数参数列表中的 10 没有任何意义。它只是被忽略了。因此最好清楚地记录下来,迭代器或指针需要提供对足够多值的访问。

VS2008 gives me a warning about the double to float conversion. How do I get rid of this warning?

void some_function(double *darray){
    vector<double> fvector;
    fvector.insert(fvector.begin(), darray, darray + 10);
}

Or

void some_function(float *darray){
    vector<float> fvector;
    fvector.insert(fvector.begin(), darray, darray + 10);
}

Or overload your function with both variants, or make it a template

template<typename RandomAccessIterator>
void some_function(RandomAccessIterator it){
    typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
    vector<value_type> fvector(it, it + 10);
}

Of course, it's not a good idea to assume a fixed 10. Notice that neither your original code is safe, because the 10 in the parameter list of your function has no meaning. It's simply ignored. So better document it clearly, that the iterator or pointer needs to provide access to sufficiently many values.

绻影浮沉 2024-09-15 14:46:24

我认为还有另一个问题。向量没有足够的空间...

In my opinion there is an other problem. The vector has not enough space...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文