print(std::ostream)
来自cppreference.com
                    
                                        
                    < cpp | io | basic ostream
                    
                                                            
                    |   在标头  <ostream> 定义
  | 
||
|   template< class... Args > void print( std::ostream& os, std::format_string<Args...> fmt, Args&&... args );  | 
(C++23 起) | |
用 args 填充格式字符串 fmt, 将结果写入输出流 os 。
 如果 通常字面量编码 是 UTF-8, 上式等价于:
std::vprint_unicode(os, fmt.get(), std::make_format_args(std::forward<Args>(args)...));
否则等价于:
std::vprint_nonunicode(os, fmt.get(), std::make_format_args(std::forward<Args>(args)...));
若 Args 中有一 Ti,其 std::formatter<Ti, char> 不满足 基本格式化器 (BasicFormatter)  要求,则行为未定义(正如 std::make_format_args 所要求的)
参数
| os | - | 数据输出流 | 
| fmt | - |  
 每个替换域拥有下列格式: 
 arg-id 指定用于格式化其值的  格式说明由对应参数特化的 std::formatter 定义。 
  | 
| args... | - | 参与格式化的参数 | 
返回值
(无)
异常
Template:std print ostream exceptions
注解
| 功能特性测试宏 | 值 | 标准 | 注释 | 
|---|---|---|---|
__cpp_lib_print | 
202207L | (C++23) | 格式化输出 | 
__cpp_lib_format | 
202207L | (C++23) | 暴露 std::basic_format_string | 
示例
测试链接: Compiler Explorer
运行此代码
#include <array> #include <cctype> #include <format> #include <ranges> #include <numbers> #include <iostream> int main() { std::array<char, 24> buf; std::format_to(buf.begin(), "{:.15f}", std::numbers::sqrt2); unsigned num{}, sum{}; auto v = buf | std::views::filter(isdigit) | std::views::transform([](char x) { return x - '0'; }) | std::views::take_while([&sum](char) { return sum < 42; }) ; for (auto n : v) { sum += n; ++num; } #ifdef __cpp_lib_print std::print( std::cout, "√2 = {}...\n" "其前 {} 位的和是 {}{}\n", std::numbers::sqrt2, num, sum, '.' ); #else std::cout << std::format( "√2 = {}...\n" "其前 {} 位的和是 {}{}\n", std::numbers::sqrt2, num, sum, '.' ); #endif }
输出:
√2 = 1.4142135623730951... 其前 13 位的和是 42.
参阅
|    (C++23)  | 
   将参数的 格式化 表达输出到 stdout 或文件缓冲区  (函数模板)  | 
|    (C++20)  | 
  在新 string 中存储参数的格式化表示  (函数模板)  |