msvc,format的使用

// CMakeProject1.h : Include file for standard system include files,
// or project specific include files.

#pragma once

#include <iostream>
#include <format>
#include <cstdio>
#include <string_view>
// TODO: Reference additional headers your program requires here.
// CMakeProject1.cpp : Defines the entry point for the application.
//

#include "CMakeProject1.h"
using namespace std;

// 实现print函数
template<typename... Args> 
void print(const string_view fmt_str, Args && ... args)
{
    auto fmt_args{ make_format_args(args...) };
    string outstr{ vformat(fmt_str,fmt_args) };
    fputs(outstr.c_str(), stdout);
}


int main()
{
    // format库现在只在msvc上实现,format() 函数本身返回一个字符串对象
    string who{ "everyone" };
    cout << format("hello {}\n", who) << endl;
    int ival{ 42 };
    cout << format("integer value: {}\n", ival) << endl;
    double pi{ 3.1415196};
    cout << format("pi: {}\n", pi) << endl;
    cout << format("hello {1}, {0}", "welcome","there") << endl;// 顺序
    cout << format("{:.>10}", ival) << endl;// 十个位,右对齐
    cout << format("{:.<10}", ival) << endl;// 左对齐
    cout << format("{:.^10}", ival) << endl;
    cout << format("pi: {:.5}", pi) << endl;// 精度为5
    print("hello {}\n", who);// print 就是 (cout << format)整个过程的实现

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值