std::print
std::print (형식 문자열 출력)
형식 문자열 fmt에 따라 args를 형식화하고 결과를 출력 스트림에 출력하는 함수예요. C++23부터 있어요.
출처: cppreference
본문
<print> 헤더에 정의돼 있어요.
// (1) C++23
template< class... Args >
void print( std::format_string<Args...> fmt, Args&&... args );
// (2) C++23
template< class... Args >
void print( std::FILE* stream,
std::format_string<Args...> fmt, Args&&... args );
형식 문자열 fmt에 따라 args를 형식화하고 결과를 출력 스트림에 출력해요.
-
std::print(stdout, fmt, std::forward<Args>(args)...)와 동등해요.
-
- 보통 문자열 인코딩이 UTF-8이면
(std::enable_nonlocking_formatter_optimization<std::remove_cvref_t<Args>> && ...)인 동안std::fputs(std::format(fmt, ...).c_str(), stream)과 동등해요. 그렇지 않으면std::fwrite를 사용해 적절한 변환을 수행해요.
- 보통 문자열 인코딩이 UTF-8이면
std::print는 출력을 버퍼링하지 않는다는 점이 std::cout과 달라요. std::cout에 출력하려면 std::print(std::cout, ...)가 아니라 std::println(std::cout, ...) 또는 포맷을 쓰는 방식을 권장해요. 실제로 printf 계열 C 스트림에 직접 출력해요.
예제
#include <print>
int main()
{
std::print("Hello, {}!\n", "world"); // "Hello, world!"
std::print("Value: {:.2f}\n", 3.14159); // "Value: 3.14"
}