std:: vformat
|
Definiert in Header
<format>
|
||
|
std::
string
vformat
(
std::
string_view
fmt,
std::
format_args
args
)
;
|
(1) | (seit C++20) |
|
std::
wstring
vformat
(
std::
wstring_view
fmt,
std::
wformat_args
args
)
;
|
(2) | (seit C++20) |
|
std::
string
vformat
(
const
std::
locale
&
loc,
std:: string_view fmt, std:: format_args args ) ; |
(3) | (seit C++20) |
|
std::
wstring
vformat
(
const
std::
locale
&
loc,
std:: wstring_view fmt, std:: wformat_args args ) ; |
(4) | (seit C++20) |
Formatieren Sie die in args enthaltenen Argumente gemäß der Formatzeichenkette fmt und geben Sie das Ergebnis als String zurück. Falls vorhanden, wird loc für lokalisierungsabhängige Formatierung verwendet.
Inhaltsverzeichnis |
Parameter
| fmt | - |
ein Objekt, das die Formatzeichenkette repräsentiert. Die Formatzeichenkette besteht aus
Jedes Ersetzungsfeld hat folgendes Format:
1)
Ersetzungsfeld ohne Formatangabe
2)
Ersetzungsfeld mit Formatangabe
|
||||||||||||||||||||||||||||||||||||||||||||||
| args | - | zu formatierende Argumente | ||||||||||||||||||||||||||||||||||||||||||||||
| loc | - | std::locale für lokalisierungsspezifische Formatierung | ||||||||||||||||||||||||||||||||||||||||||||||
Rückgabewert
Ein String-Objekt, das das formatierte Ergebnis enthält.
Exceptions
Wirft std::format_error falls fmt keine gültige Formatzeichenkette für die bereitgestellten Argumente ist, oder std::bad_alloc bei Speicherallokierungsfehlern. Leitet auch alle Ausnahmen weiter, die von Formatierer- oder Iterator-Operationen ausgelöst werden.
Beispiel
#include <format> #include <iostream> template<typename... Args> inline void println(const std::format_string<Args...> fmt, Args&&... args) { std::cout << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n'; } int main() { println("{}{} {}{}", "Hello", ',', "C++", -1 + 2 * 3 * 4); }
Ausgabe:
Hello, C++23