std::print(std::ostream)

Header: <ostream>

Formats args according to the format string fmt, and inserts the result into os stream.

# Declarations

template< class... Args >
void print( std::ostream& os, std::format_string<Args...> fmt, Args&&... args );

(since C++23)

# Parameters

# Return value

(none)

# Notes

Feature-test macro Value Std Feature __cpp_lib_print 202207L (C++23) Formatted output __cpp_lib_format 202207L (C++23) Exposing std::basic_format_string

# Example

#include <array>
#include <cctype>
#include <cstdio>
#include <format>
#include <numbers>
#include <ranges>
#include <sstream>
 
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;
 
    std::stringstream stream;
 
#ifdef __cpp_lib_print
    std::print(stream,
#else
    stream << std::format(
#endif
        "√2 = {}...\n"
        "The sum of its first {} digits is {}{}",
        std::numbers::sqrt2, num, sum, '.'
    );
 
    std::puts(stream.str().data());
}

# See also