3

我正在尝试将数据从一个容器传输到另一个容器:

#include <vector>
int main()
{
    std::vector<int>   input_data;
    std::vector<float> output_data;

    output_data.insert(output_data.end(), input_data.begin(), input_data.end());
}

在 VS2005 中,我在 base.hpp 中收到三个 C4244 警告,说“从 'const int' 转换为 'const float',可能丢失数据。”

现在我理解了这个警告,这是一个合法的警告。但是在我的特殊情况下,数据丢失可以忽略不计。有没有办法在不必做这样的循环的情况下确认警告?

for (std::vector<int>::const_iterator it; it != input_data.end(); ++it)
{
    output_data.push_back(static_cast<float32>(*it));
}
4

2 回答 2

5

In Visual Studio, you should be able to get around the warning by using the #pragma directives (it'd be perfectly valid to do this, as you are aware of the warning and just wish to suppress it), for instance, your main function would look something like:

#include <vector>
int main()
{
    std::vector<int>   input_data;
    std::vector<float> output_data;

#pragma warning(suppress: 4244)
    output_data.insert(output_data.end(), input_data.begin(), input_data.end());
}

This will suppress the warning C4244 for the following line of code (subsequent or preceding lines will still emit that warning). If you want to disable warning emissions for larger blocks of code, you may wish to look at the other #pragma warning directives.

于 2013-07-23T16:24:19.813 回答
1

boost::transform_iterator与现有的插入物一起使用怎么样?

struct to_float { float operator()(int x) const { return static_cast<float>(x); };
output_data.insert(output_data.end(), boost::make_transform_iterator(input_data.begin(), to_float()), boost::make_transform_iterator(input_data.end(), to_float()));
于 2013-07-23T16:30:58.487 回答