logo

C++ での数値から文字列への変換

C++ では、整数を文字列に変換したり、数値を文字列に変換したり、その逆を行うこと自体が、実際には大きなパラダイム シフトです。一般に、具体的には競技プログラミングでは、数値を文字列に、または文字列を数値に変換する必要がある場合が多くあります。整数または数値を文字列に変換するいくつかのメソッドを見てみましょう。

C++ での数値から文字列への変換

がある 数値を文字列に変換する 4 つの主な方法 、それは次のとおりです。



    to_string() の使用 string Stream の使用 sprintf() 関数の使用 ブースト字句キャストの使用

方法 1: to_string() を使用する

to_string() 関数を使用すると、整数、浮動小数点値、または任意の数値を文字列に変換できます。この関数は数値 (任意のデータ型) を受け取り、その数値を目的の文字列として返します。

構文 :

string  to_string (int val );>

パラメーター:



  • ヴァル – 任意の数値。

戻り値:

  • 値の表現を文字のシーケンスとして含む文字列オブジェクト。

:

C++






// C++ code to demonstrate 'to_string()' method> // to convert number to string.> #include> #include // for string and to_string()> using> namespace> std;> // Driver Code> int> main()> {> >// Declaring integer> >int> i_val = 20;> >// Declaring float> >float> f_val = 30.50;> >// Conversion of int into string using> >// to_string()> >string stri = to_string(i_val);> >// Conversion of float into string using> >// to_string()> >string strf = to_string(f_val);> >// Displaying the converted strings> >cout <<>'The integer in string is : '>;> >cout << stri << endl;> >cout <<>'The float in string is : '>;> >cout << strf << endl;> >return> 0;> }>

>

>

出力

The integer in string is : 20 The float in string is : 30.500000>

時間計算量: の上)
補助スペース: の上)

方法 2: 文字列ストリームを使用する

このメソッドでは、文字列ストリームがストリーム オブジェクトを宣言します。このストリーム オブジェクトは、最初に数値をストリームとしてオブジェクトに挿入し、次に使用します。 str() 数値から文字列への内部変換を追跡します。

例:

C++




// C++ code to demonstrate string stream method> // to convert number to string.> #include> #include // for string streams> #include // for string> using> namespace> std;> int> main()> {> >int> num = 2016;> >// declaring output string stream> >ostringstream str1;> >// Sending a number as a stream into output> >// string> >str1 << num;> >// the str() converts number into string> >string geek = str1.str();> >// Displaying the string> >cout <<>'The newly formed string from number is : '>;> >cout << geek << endl;> >return> 0;> }>

>

>

出力

The newly formed string from number is : 2016>

時間計算量: の上)
補助スペース: の上)

方法 3: sprintf() 関数を使用する

スプリントf() この関数は、出力をコンソールに出力するのではなく、関数で指定された文字バッファーに出力を保存します。

C++




// C++ Program to illustrate the use of sprintf() for number> // to string conversion> #include> using> namespace> std;> int> main()> {> >// any num> >int> n = 12234;> >// string buffer> >char> str[1000];> >// sprintf() to print num to str buffer> >sprintf>(str,>'%d'>, n);> >cout <<>'the string is : '> << str;> >return> 0;> }> // this code is contributed by shivanisingh>

>

>

出力

the string is : 12234>

時間計算量: の上)
補助スペース: の上)

方法 4: ブースト字句キャストの使用

文字列変換と同様に、lexical_cast() 関数は同じままですが、' 語彙キャストを強化する ' 時刻引数リストは lexical_cast(numeric_var) に変更されます。

例:

C++


arpコマンド



// C++ code to demonstrate 'lexical_cast()' method> // to convert number to string.> #include // for lexical_cast()> #include> #include // for string> using> namespace> std;> // Driver Code> int> main()> {> >// Declaring float> >float> f_val = 10.5;> >// Declaring int> >int> i_val = 17;> >// lexical_cast() converts a float into string> >string strf = boost::lexical_cast(f_val);> >// lexical_cast() converts a int into string> >string stri = boost::lexical_cast(i_val);> >// Displaying string converted numbers> >cout <<>'The float value in string is : '>;> >cout << strf << endl;> >cout <<>'The int value in string is : '>;> >cout << stri << endl;> >return> 0;> }>

>

>

出力

The float value in string is : 10.5 The int value in string is : 17>

時間計算量 : の上)
補助スペース : の上)