用户输入的 stol 和 stoi 上的 C++ 精度

C++ Precision on stol and stoi for user input

试图从字符串中解析浮点数,它 returns 精度为 0 ,我正在寻找 0.0 。

代码:

queue<Car> getInputData() {
    string input;
    string line;
    string delin = " ";
    string::size_type sz = 0.0; 
    int counter = 0;
    queue<Car> cars;
    while (getline(std::cin, line) && !line.empty()) 
        {
        if (counter != 0)
            {
            queue<string> parse = getStringList(line, " ");
            _directions dir;
            int cid = std::stoi(parse.front());
            parse.pop();
            long arriv = std::stoi(parse.front()); //This returns 1 rather than 1.1
            parse.pop();
            dir.start = parseToDirection(parse.front(), true);
            parse.pop();
            dir.end = parseToDirection(parse.front(), false);
            parse.pop();
            Car car = Car(dir, cid, arriv);
            cars.push(car);

        }
        counter++;
    }
return cars; 
}

示例输入:

cid arrival_time dir_original dir_target

0 1.1 ^ ^

1 2.0 ^ ^

2 3.3 ^ <

3 3.5 v v

4 4.2 v >

5 4.4 ^ ^

6 5.7 > ^

7 5.9 < ^

我尝试过的: 我试过 stol、stoi、stold、stod,其中 none 用于精确度。这可能吗?还是我缺少解决方法?

如果你想解析一个浮点数,你需要使用stod(或者stof或者stold,但是stod通常是你想要的) .并且需要将结果赋给一个类型允许浮点数的变量,通常是double。 (floatlong double对应stofstold。)

long是整数类型。