C++ 字符格式化输出

文章目录

一、简介

这里使用std标准库简单实现一个字符格式化输出,方便后续的使用,它有点类似Qt中的QString操作。

二、实现代码

FMTString.hpp

cpp 复制代码
#pragma once

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <optional>
#include <random>
#include <type_traits>
#include <sstream>

namespace FMT 
{
    void formatStream(std::stringstream& stream, char const* text) {
        stream << text;
        return;
    }

    /// <summary>
    /// 在编译时检测某个类型T是否支持通过 operator<< 输出到流(如std::stringstream)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    template <class T>
    class IsStreamable {
    private:
        template <class TT>
        static auto test(int)
            -> decltype(std::declval<std::stringstream&>() << std::declval<TT>(),
                std::true_type());

        template <class>
        static auto test(...)->std::false_type;

    public:
        static bool const value = decltype(test<T>(0))::value;
    };

    /// <summary>
    /// 将任意类型的参数通过流操作符 << 转换为字符串流(std::stringstream)的内容
    /// </summary>
    /// <typeparam name="T"></typeparam>
    template <class T>
    class ArgToStream {
    public:
        static void impl(std::stringstream& stream, T&& arg) {
            stream << std::forward<T>(arg);
        }
    };

    /// <summary>
    /// 将格式字符串 text 中的 {} 占位符依次替换为传入的参数 arg, args...,并将结果写入 std::stringstream 流中
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="...Args"></typeparam>
    /// <param name="stream"></param>
    /// <param name="text"></param>
    /// <param name="arg"></param>
    /// <param name="...args"></param>
    template <class T, typename... Args>
    void formatStream(std::stringstream& stream, char const* text, T&& arg,
        Args&&... args) {
        static_assert(IsStreamable<T>::value,
            "One of the args has no ostream overload!");
        for (; *text != '\0'; ++text) {
            if (*text == '{' && *(text + 1) == '}') {
                ArgToStream<T&&>::impl(stream, std::forward<T>(arg));
                formatStream(stream, text + 2, std::forward<Args>(args)...);
                return;
            }
            stream << *text;
        }
        stream << "\nFormat-Warning: There are " << sizeof...(Args) + 1
            << " args unused.";
        return;
    }

    template <class... Args>
    std::string formatString(char const* text, Args&&... args) {
        std::stringstream stream;
        formatStream(stream, text, std::forward<Args>(args)...);
        return stream.str();
    }
}

main.cpp

cpp 复制代码
// 标准文件
#include <iostream>
#include <string>

#include "FMTString.hpp"

int main(int, char** argv)
{
    std::string msg = FMT::formatString("Hello, {}! You have {} new messages.", "Alice", 5);
    std::cout << "Output:" << msg << std::endl;

    system("pause");
    return 0;
}

三、实现效果

相关推荐
肆忆_16 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星20 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc5 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼5 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛