C++20 bietet eine Vielzahl neuer Funktionen und Verbesserungen, darunter Format() Funktion. In diesem Artikel untersuchen wir, wie std::format zum Formatieren von Zeichenfolgen in C++20 verwendet werden kann.
C++20 – std::format
std::format ist eine neue Funktion, die in C++20 eingeführt wurde und eine Möglichkeit zum Formatieren von Zeichenfolgen bietet, indem Platzhalter innerhalb einer Formatzeichenfolge durch die Werte der bereitgestellten Argumente ersetzt werden. Die Platzhalter werden mit {} innerhalb der Formatzeichenfolge dargestellt.
Syntax:
std::string std::format(std::string_view format_string, Args... args);>
Rückgabetyp: Die Funktion gibt einen std::string zurück, der die formatierte Ausgabe enthält.
In C++20 wird ein neuer Datentyp namens std::string_view eingeführt, der eine Ansicht des zugrunde liegenden Strings bereitstellt. Es funktioniert ähnlich wie ein Zeiger auf einen String, verfügt jedoch über zusätzliche Sicherheits- und Komfortfunktionen. Die Args… stellen einen variablen Parameter dar, was bedeutet, dass die Funktion std::format eine variable Anzahl von Argumenten beliebiger Art annehmen kann.
Beispiele für C++ 20 – std::format
Beispiel 1:
Der folgende Code zeigt, wie Sie mit std::format eine Zeichenfolge mit Platzhaltern für Variablen formatieren.
C++
Fuchs oder Wolf
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > using> namespace> std;> > int> main()> {> >// Declare variables> >int> num = 42;> >std::string name =>'John'>;> > >// Use std::format to format a string with placeholders> >// for variables> >std::string formatted_str = std::format(> >'My name is {} and my favorite number is {}'>, name,> >num);> > >// Print formatted string to console> >std::cout << formatted_str << std::endl;> > >return> 0;> }> |
>
Java-Sortier-Arrayliste
>
Ausgabe
My name is John and my favorite number is 42>
Im obigen Beispiel haben wir eine Formatzeichenfolge, die zwei Platzhalter enthält, {}. Wir übergeben die Werte der Variablen name und num an die Funktion std::format, die die Platzhalter durch die Werte der Variablen ersetzt. Die resultierende Zeichenfolge wird in der Variablen formatted_str gespeichert, die dann auf der Konsole ausgegeben wird.
Beispiel 2:
Im folgenden Beispiel werden wir die Positionsargumente mit std::format verstehen.
C++
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > int> main()> {> >// Declare an integer variable named num> >// and initialize it with the value 42> >int> num = 42;> >// Declare a string variable named name> >// and initialize it with the value 'John'> >std::string name =>'John'>;> > >// Call the std::format function to create a formatted> >// string with placeholders for num and name The first> >// placeholder is represented by {0} and is replaced by> >// the value of num The second placeholder is> >// represented by {1} and is replaced by the value of> >// name> >std::string formatted_str = std::format(> >'My name is {1} and my favorite number is {0}'>, num, name);> > >// Print the formatted string to the console> >std::cout << formatted_str << std::endl;> >return> 0;> }> |
>
Gimp-Schriftartenliste
>
Ausgabe
My name is John and my favorite number is 42>
Im obigen Beispiel haben wir die Reihenfolge der Argumente in der Funktion std::format umgekehrt und den Platzhaltern Positionsindizes hinzugefügt. Der erste Platzhalter wird durch den Wert „num“ und der zweite Platzhalter durch den Wert „name“ ersetzt.
Beispiel 3:
Im folgenden Beispiel werden wir sehen, wie std::format viele Optionen zum Formatieren von Zeichenfolgen bereitstellt, wobei wir die Platzhalter {} verwenden können, um Formatierungsoptionen für jedes Argument anzugeben.
C++
// C++ Program to implement> // C++ 20 - std::format> #include> #include> > int> main()> {> > >// Declare and initialize a double variable.> >double> num = 3.14159;> > >// Declare and initialize a string variable.> >std::string name =>'John'>;> > >// Format a string with two placeholders, one for a> >// double and another for a string. The first> >// placeholder formats the double with two decimal> >// places and the second placeholder truncates the> >// string to two characters.> >std::string formatted_str = std::format(> >'My name is {1:.2s} and pi is {0:.2f}'>, num, name);> > >// Print the formatted string to the console.> >std::cout << formatted_str << std::endl;> > >return> 0;> }> |
>
>
Ausgabe
My name is Jo and pi is 3.14>
Im obigen Beispiel haben wir den Platzhaltern Formatierungsoptionen hinzugefügt. Der erste Platzhalter ist als Gleitkommazahl mit zwei Dezimalstellen formatiert und der zweite Platzhalter ist als Zeichenfolge mit einer maximalen Breite von zwei Zeichen formatiert.
js globale Variable
Abschluss
std::format unterstützt eine Vielzahl von Formatierungsoptionen, einschließlich der Möglichkeit, benutzerdefinierte Typen zu formatieren. Es ist effizienter als frühere Zeichenfolgenformatierungsoptionen in C++, wie z. B. sprintf und printf.