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;
}
相关推荐
萌动的小火苗12 分钟前
1、python基础面试题
java·开发语言·python
运维行者_21 分钟前
网络监控与ITSM集成:从告警到工单,实现运维自动化闭环
开发语言·网络·分布式·后端·架构·flask·php
小叮当爱咖啡1 小时前
Day3.参数+Prompt三板斧
开发语言·python·prompt
菜冻鱼1 小时前
Python-pandas-索引与筛选
开发语言·笔记·python·numpy·pandas·学习方法
我想走路带风1 小时前
各自缓冲及实现
c++
鹿角片ljp1 小时前
Java面试复盘(二):String不可变、字符串常量池与字符串拼接
开发语言·python
小秋求学记.2 小时前
PHP安全漏洞实战:从环境配置到RCE攻击
开发语言·php
阿米亚波2 小时前
【C/C++包管理器】vcpkg(by microsoft)
c语言·c++·git·vscode·microsoft·github·vcpkg
看浪的路人3 小时前
第3讲:代码补全引擎
开发语言·windows·python
SNAKEpc121383 小时前
OpenGL(十一)- 变换管线
c语言·c++·算法·矩阵·图形渲染