Cpp(八) 格式化输出 format

2021-02-02 15:43:25 浏览数 (2)

C fmt

#0 代码

  • 本文例子代码
代码语言:javascript复制
https://github.com/Coxhuang/FKCpp/tree/master/fmt_demo
  • fmt官方文档
代码语言:javascript复制
https://github.com/fmtlib/fmt

#1 环境

代码语言:javascript复制
C  14
CMake 3.17
macOS 10.15.5(Ubuntu 16.04 测试成功)
Clion

#2 开始

#2.1 安装fmt

代码语言:javascript复制
git clone  https://github.com/fmtlib/fmt.git
cmake .
make && make install 

#2.2 配置cmake

代码语言:javascript复制
cmake_minimum_required(VERSION 3.17)
project(fmt_demo)

set(CMAKE_CXX_STANDARD 14)

include_directories(/usr/local/include) # 添加头文件搜索路径

link_directories(/usr/local/lib) # 添加库文件搜索路径

add_executable(fmt_demo main.cpp)

target_link_libraries(fmt_demo fmt)

#2.3 使用

代码语言:javascript复制
#include "fmt/format.h"
#include <fmt/core.h>
#include <fmt/chrono.h>

using namespace std::literals::chrono_literals;

int main() {
    fmt::print("Hello, world!n");
    std::string s = fmt::format("The answer is {}.", 42);
    fmt::print(s);

    fmt::print("Default format: {} {}n", 42s, 100ms);
    fmt::print("strftime-like format: {:%H:%M:%S}n", 3h   15min   30s);
    return 0;
}

0 人点赞