【问题标题】:Read multiple comma separated doubles from a string从字符串中读取多个逗号分隔的双精度
【发布时间】:2012-02-19 13:34:34
【问题描述】:

我需要从一个字符串中获取一些双精度值。

string data = getMyData();
char** next;
double start = strtod(data.c_str(), next);

if (&data == &(*next)) //check wether a double has been found - not working
{
    std::cerr << "Value can't be read.\nAborting.";
    return;
}

我的想法是检查数据的第一个字符和下一个字符的内存地址。 目前我正在自学中学习 C++,所以如果能得到最好的解决方案,而不仅仅是一个可行的解决方案,那就太好了。

【问题讨论】:

    标签: c++ string double type-conversion


    【解决方案1】:

    应该是:

    char* next;
    double start = strtod(data.c_str(), &next);
    
    if (data.c_str() == next)
    

    请记住,next 将指向下一个 逗号(如果这些是逗号分隔的),而不是下一个数字的开头。

    【讨论】:

    • 是的,我知道。这是我的下一步。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多