C++20 には、次のような多数の新機能と改善点が備わっています。 フォーマット() 関数。この記事では、C++20 で std::format を使用して文字列をフォーマットする方法を検討します。
C++20 – std::format
std::format は、C++20 で導入された新しい関数で、フォーマット文字列内のプレースホルダーを指定された引数の値に置き換えることによって文字列をフォーマットする方法を提供します。プレースホルダーは、形式文字列内で {} を使用して表されます。
構文:
std::string std::format(std::string_view format_string, Args... args);>
戻り値の型: この関数は、フォーマットされた出力を含む std::string を返します。
C++20 では、std::string_view という名前の新しいデータ型が導入され、基になる文字列のビューが提供されます。これは文字列へのポインターと同様に機能しますが、追加の安全性と利便性の機能を備えています。 Args… は可変個引数パラメーターを表します。これは、 std::format 関数が任意の型の可変数の引数を取ることができることを意味します。
mylivecricket ライブクリケットに参加します
C++ 20 の例 – std::format
例 1:
次のコードは、 std::format を使用して、変数のプレースホルダーを含む文字列をフォーマットする方法を示しています。
C++
// 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;> }> |
>
>
出力
My name is John and my favorite number is 42>
上の例では、2 つのプレースホルダー {} を含むフォーマット文字列があります。変数 name と num の値を std::format 関数に渡し、プレースホルダーを変数の値に置き換えます。結果の文字列は変数 formatted_str に保存され、コンソールに出力されます。
例 2:
次の例では、std::format の位置引数を理解します。
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;> }> |
>
日付形式.形式
>
出力
My name is John and my favorite number is 42>
上記の例では、std::format 関数の引数の順序を逆にし、プレースホルダーに位置インデックスを追加しました。最初のプレースホルダーは num の値に置き換えられ、2 番目のプレースホルダーは name の値に置き換えられます。
例 3:
次の例では、std::format が文字列をフォーマットするための多くのオプションをどのように提供するかを示します。ここでは、{} プレースホルダーを使用して各引数のフォーマット オプションを指定できます。
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;> }> |
>
>
文字列をint型Javaに変換する
出力
My name is Jo and pi is 3.14>
上の例では、プレースホルダーに書式設定オプションを追加しました。最初のプレースホルダーは小数点以下 2 桁の浮動小数点数としてフォーマットされ、2 番目のプレースホルダーは最大幅 2 文字の文字列としてフォーマットされます。
結論
std::format は、ユーザー定義型の書式設定機能を含む、幅広い書式設定オプションをサポートしています。これは、sprintf や printf などの C++ の以前の文字列書式設定オプションよりも効率的です。