std::format
提供: cppreference.com
ヘッダ <format> で定義
|
||
template<class... Args> std::string format(std::string_view fmt, const Args&... args); |
(1) | (C++20以上) |
template<class... Args> std::wstring format(std::wstring_view fmt, const Args&... args); |
(2) | (C++20以上) |
template<class... Args> std::string format(const std::locale& loc, std::string_view fmt, const Args&... args); |
(3) | (C++20以上) |
template<class... Args> std::wstring format(const std::locale& loc, std::wstring_view fmt, const Args&... args); |
(4) | (C++20以上) |
書式文字列 fmt
に従って args
を書式化し、その結果を文字列として返します。 存在する場合はロケール固有の書式化のために loc
が使用されます。
std::formatter<Ti, CharT> が Args
内の任意の Ti
について Formatter の要件を満たさない場合、動作は未定義です。 ただし CharT
は decltype(fmt)::char_type (オ��バーロード (1,3) の場合は char
、オーバーロード (2,4) の場合は wchar_t
) です。
目次 |
[編集] 引数
fmt | - | 書式化文字列を表す文字列ビュー。
置換フィールドは以下の形式を持ちます。
arg-id は書式化に使用する値を持つ 書式指定は対応する引数のための std::formatter の特殊化によって定義されます。
|
args... | - | 書式化する引数。 |
loc | - | ロケール固有の書式化のために使用される std::locale。 |
[編集] 戻り値
書式化した結果を保持する文字列オブジェクト。
[編集] 例外
fmt
が有効な書式文字列でない場合、 std::format_error を投げます。 いずれかのフォーマッタによって投げられた例外も伝播されます。
[編集] 例
Run this code
#include <iostream> #include <format> int main() { std::cout << std::format("Hello {}!\n", "world"); }
出力:
Hello world!