浮点数C ++,如何在数组的每个元素的最后添加“ f”

问题描述 投票:0回答:1

我刚刚打开filename.file,将每个浮点数保存在矢量“顶点”中,但将它们另存为整数数。需要将所有数字正确转换为浮点数。例如:从“ 1”到“ 1.00000f”,或者从“ 1”到“ 1.0f”。

/////filename.file
...
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
...
//////


std::vector<float> vertices;

std::ifstream file(filename); 

    std::string line;

    while (std::getline(file, line))
    {

        std::istringstream iss(line);

        std::string result;

        if (std::getline(iss, result, ' '))
        {

            if (result == "v")
            {



                while (std::getline(iss, token, ' '))
                {
                    std::istringstream iss1(token);

                    if (std::getline(iss1, word))
                    {
                        float word_float = std::stof(word);

                        //std::cout << word_float << std::endl;
                        vertices.push_back(word_float);
                    }
                }
            }
        }
    }

//Look what I got
for(std::size_t i = 0 ; i < vertices.size(); i++) {
 std::cout << vertices[i] << " ";
}

/ *每个元素1个-11个...

//但是这些数字应该完全这样保存

1.000000f-1.000000f1.000000f...* /

c++ arrays vector floating-point floating
1个回答
0
投票

听起来你想要一些东西,顺序是:

std::cout << std::fixed << std::setprecision(5) << value << "f";

例如:

float values[] = { 1.0f, -1.0f, 1.0f };

for (auto const &value : values)
    std::cout << std::fixed << std::setprecision(5) << value << "f\t";

结果:

1.00000f    -1.00000f   1.00000f
© www.soinside.com 2019 - 2024. All rights reserved.