csv-parser在C++17下from_chars函数问题

前言:

哎!c++碎片化,真是麻烦!

我win下vs2017的C++17没问题.

linux下 g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0有问题.即使c++17

这就牵扯到各家编译时实现的不同.

解决办法:

参考我的
https://github.com/vincentlaucsb/csv-parser/issues/257#issuecomment-2642703294

C++17的std::from_chars最初只支持整数类型,比如int、long等,而对浮点数类型的支持是在C++20中添加的。所以可能在C++17中,确实没有针对浮点数的from_chars函数,导致编译器找不到对应的重载版本,从而报错。

用这个函数代替原先的get_max函数

cpp 复制代码
long double get_max(std::string file, std::string column, bool use_std = false);


long double get_max(std::string file, std::string column, bool use_std) {
    using namespace csv;
    long double max = -std::numeric_limits<long double>::infinity();
    CSVReader reader(file);

    for (auto& row : reader) {
        auto field = row[column];
        long double out = 0;

        if (use_std) {
            auto _field = field.get<std::string_view>();
            const char* data = _field.data();
            char* end;
            errno = 0; // 重置错误标志
            out = std::strtold(data, &end);
            
            // 检查转换是否成功
            if (data == end) {
                // 没有数字被转换,处理错误
                std::cerr << "转换失败: '" << _field << "'" << std::endl;
                continue; // 跳过当前行
            } else if (errno == ERANGE) {
                // 处理溢出情况
                if (out == HUGE_VALL) {
                    out = std::numeric_limits<long double>::infinity();
                } else if (out == -HUGE_VALL) {
                    out = -std::numeric_limits<long double>::infinity();
                }
            }
        } else {
            out = field.get<long double>();
        }

        if (out > max) {
            max = out;
        }
    }

    return max;
}
相关推荐
xuxigifxfh12 分钟前
HJ11 数字颠倒
java·开发语言·华为机考
一只旭宝29 分钟前
数据结构与算法复习手册:从写过,到讲清楚
开发语言·数据结构·c++·算法
百度一下吧30 分钟前
断网后 WebView 加载单页面应用:页面跳转 JS 加载失败的处理方案
开发语言·javascript·ecmascript
十五年专注C++开发34 分钟前
std::filesystem::exists 在有的win10系统抛异常原因分析
c++·文件系统·filesystem
神仙别闹1 小时前
基于C语言开发的捕捉流星小游戏
c语言·开发语言
程序员Sunday1 小时前
Promise.all、allSettled、race、any 怎么选,失败后其他请求会怎样
开发语言·前端·javascript
hetao17338371 小时前
2026-10-07 hetao1733837 的刷题记录
c++·算法
daxiangxm1 小时前
GEO策略:让AI直接引用你的内容
开发语言·团队开发·运维开发·个人开发·数据库开发
泡海椒2 小时前
JQuick-Excel 实战:用 VALIDATION 建立可维护的 Excel 导入校验
开发语言·python·excel
longlongzihan2 小时前
前缀积与后缀积 —— LeetCode 238. 除了自身以外数组的乘积
开发语言·c++·leetcode