标准库标头 <charconv>(c++17)学习

此头文件是strings library.

std::to_chars_resultstd::to_chars 的返回类型。它没有基类,并且只有以下成员。

数据成员

成员名字定义

ptr

char* 类型的指针
(公开成员对象)

ec

std::errc 类型的错误码
(公开成员对象)

std::from_chars_resultstd::from_chars 的返回类型。它没有基类,并且只有以下成员。

数据成员

成员名字定义

ptr

const char* 类型的指针
(公开成员对象)

ec

std::errc 类型的错误码
(公开成员对象)

类型声明 

namespace std {
  // 初等数值转换的浮点格式
  enum class chars_format {
    scientific = /* 未指定 */,
    fixed = /* 未指定 */,
    hex = /* 未指定 */,
    general = fixed | scientific
  };
 
 
  // 初等数值输出转换
  struct to_chars_result { // 独立
    char* ptr;
    errc ec;
    friend bool operator==(const to_chars_result&, const to_chars_result&) = default;
    constexpr explicit operator bool() const noexcept { return ec == errc{}; }
  };
 
  to_chars_result to_chars(char* first, char* last, // 独立
                           /* integer-type */ value, int base = 10);
  to_chars_result to_chars(char* first, char* last, // 独立
                           bool value, int base = 10) = delete;
  to_chars_result to_chars(char* first, char* last, // 独立
                           /* floating-point-type */ value);
  to_chars_result to_chars(char* first, char* last, // 独立
                           /* floating-point-type */ value, chars_format fmt);
  to_chars_result to_chars(char* first, char* last, // 独立
                           /* floating-point-type */ value,
                           chars_format fmt, int precision);
 
  // 初等数值输入转换
  struct from_chars_result { // 独立
    const char* ptr;
    errc ec;
    friend bool operator==(const from_chars_result&, const from_chars_result&) = default;
    constexpr explicit operator bool() const noexcept { return ec == errc{}; }
  };
 
  constexpr
  from_chars_result from_chars(const char* first, // 独立
                               const char* last, /* integer-type */& value,
                               int base = 10);
  from_chars_result from_chars(const char* first, // 独立或被删除
                               const char* last, /* floating-point-type */& value,
                               chars_format fmt = chars_format::general);
}

chars_format

(C++17)

指定 std::to_chars 和 std::from_chars 所用的格式
(枚举)

from_chars_result

(C++17)

std::from_chars 的返回类型
(类)

to_chars_result

(C++17)

std::to_chars 的返回类型
(类)

函数

from_chars

(C++17)

转换字符序列到整数或浮点值
(函数)

to_chars

(C++17)

转换整数或浮点值为字符序列
(函数)

示例代码:

#include <array>
#include <charconv>
#include <iostream>
#include <string_view>
#include <system_error>
#include <cassert>
#include <iomanip>
#include <optional>


//c++20
void show_to_chars(auto... format_args)
{
    std::array<char, 10> str;

#if __cpp_lib_to_chars >= 202306L
    // 使用 C++26 operator bool() 进行错误检查
    if (auto res = std::to_chars(str.data(), str.data() + str.size(), format_args...))
        std::cout << std::string_view(str.data(), res.ptr) << '\n';
    else
        std::cout << std::make_error_code(res.ec).message() << '\n';
#else
    if (auto [ptr, ec]
        = std::to_chars(str.data(), str.data() + str.size(), format_args...);
        ec == std::errc())
        std::cout << std::string_view(str.data(), ptr) << '\n';
    else
        std::cout << std::make_error_code(ec).message() << '\n';
#endif
}

int main()
{
    //to_chars example
    show_to_chars(42);
    show_to_chars(+3.14159F);
    show_to_chars(-3.14159, std::chars_format::fixed);
    show_to_chars(-3.14159, std::chars_format::scientific, 3);
    show_to_chars(3.1415926535, std::chars_format::fixed, 10);

    //form_chars example
    for (std::string_view const str : {"1234", "15 foo", "bar", " 42", "5000000000"})
    {
        std::cout << "字符串: " << std::quoted(str) << ". ";
        int result{};
        auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);

        if (ec == std::errc())
            std::cout << "结果: " << result << ", ptr -> " << std::quoted(ptr) << '\n';
        else if (ec == std::errc::invalid_argument)
            std::cout << "这不是数值。\n";
        else if (ec == std::errc::result_out_of_range)
            std::cout << "这个数值大于 int。\n";
    }

    // C++23 的 constexpr from_char 演示 / C++26 的 operator bool() 演示:
    auto to_int = [](std::string_view s) -> std::optional<int>
        {
            int value{};
#if __cpp_lib_to_chars >= 202306L
            if (std::from_chars(s.data(), s.data() + s.size(), value))
#else
            if (std::from_chars(s.data(), s.data() + s.size(), value).ec == std::errc{})
#endif
                return value;
            else
                return std::nullopt;
        };

    assert(to_int("42") == 42);
    assert(to_int("foo") == std::nullopt);
#if __cpp_lib_constexpr_charconv and __cpp_lib_optional >= 202106
    static_assert(to_int("42") == 42);
    static_assert(to_int("foo") == std::nullopt);
#endif



}

 运行结果:

参考:

标准库标头 <charconv> - cppreference.com

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值