std:: print (std::ostream)
|
헤더에 정의됨
<ostream>
|
||
|
template
<
class
...
Args
>
void print ( std:: ostream & os, std:: format_string < Args... > fmt, Args && ... args ) ; |
(C++23부터) | |
형식 문자열 fmt 에 따라 args 를 포맷하고, 결과를 os 스트림에 삽입합니다.
만약 ordinary literal encoding 이 UTF-8인 경우, 다음과 동일합니다:
- std::vprint_unicode ( os, fmt. get ( ) , std:: make_format_args ( args... ) ) ; 그렇지 않으면,
- std::vprint_nonunicode ( os, fmt. get ( ) , std:: make_format_args ( args... ) ) ; .
std::
formatter
<
Ti,
char
>
가
Args
내의 어떤
Ti
에 대해서도
BasicFormatter
요구사항을 충족하지 않을 경우(
std::
make_format_args
에 의해 요구되는 대로), 동작은 정의되지 않습니다.
목차 |
매개변수
| os | - | 데이터를 삽입할 출력 스트림 | ||||||||||||||||||||||||||||||||||||||||||||||
| fmt | - |
각 치환 필드는 다음 형식을 가집니다:
1)
형식 명세 없음 치환 필드
2)
형식 명세 포함 치환 필드
|
||||||||||||||||||||||||||||||||||||||||||||||
| args... | - | 서식 지정할 인자들 | ||||||||||||||||||||||||||||||||||||||||||||||
예외
- std::bad_alloc 할당 실패 시.
- 모든 formatter 에서 발생하는 예외(예: std::format_error )를 os. exceptions ( ) 의 값과 관계없이 전파하며, os 의 오류 상태에서 ios_base::badbit 을 설정하지 않습니다.
- os 에 대한 삽입이 실패할 경우 호출되는 os. setstate ( ios_base :: badbit ) 로 인해 발생하는 ios_base::failure 를 throw할 수 있습니다.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_print
|
202207L
|
(C++23) | 형식화된 출력 |
__cpp_lib_format
|
202207L
|
(C++23) | std::basic_format_string 노출 |
예제
#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{}; for (auto n : buf | std::views::filter(isdigit) | std::views::transform([](char x) { return x - '0'; }) | std::views::take_while([&sum](char) { return sum < 42; })) sum += n, ++num; std::stringstream stream; #ifdef __cpp_lib_print std::print(stream, #else stream << std::format( #endif "√2 \N{ALMOST EQUAL TO} {0}.\n" "The sum of its first {1} digits is {2}.", std::numbers::sqrt2, num, sum ); std::puts(stream.str().data()); }
출력:
√2 ≈ 1.4142135623730951. The sum of its first 13 digits is 42.
참고 항목
|
(C++23)
|
인수의
형식화된
표현을 출력하고
'
\n
'
을 추가함
(함수 템플릿) |
|
(C++23)
|
인수의
형식화된
표현을 사용하여
stdout
또는 파일 스트림에 출력함
(함수 템플릿) |
|
(C++20)
|
인수의 형식화된 표현을 새 문자열에 저장함
(함수 템플릿) |