En C++, convertir des entiers en chaînes ou des nombres en chaînes ou vice versa est en fait un grand changement de paradigme en soi. En général, ou plus particulièrement dans la programmation compétitive, il existe de nombreux cas où nous devons convertir un nombre en chaîne ou une chaîne en nombre. Examinons quelques méthodes pour convertir un entier ou un nombre en chaîne.
Conversion d'un nombre en chaîne en C++
Il y a 4 méthodes principales pour convertir un nombre en chaîne , qui sont les suivants :
- Utilisation de to_string() Utilisation de string Stream Utilisation de la fonction sprintf() Utilisation de la distribution lexicale boost
Méthode 1 : utiliser to_string()
Le vers_chaîne() La fonction peut être utilisée pour convertir un entier, des valeurs à virgule flottante ou n'importe quel nombre en chaîne. Cette fonction accepte un nombre (qui peut être n'importe quel type de données) et renvoie le nombre sous forme de chaîne souhaitée.
Syntaxe :
string to_string (int val );>
Paramètres:
- Val – Toute valeur numérique.
Valeur de retour :
- Un objet chaîne contenant la représentation de la valeur sous forme de séquence de caractères.
Exemple :
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;> }> |
oups, des concepts
>
cm en pieds et pouces
>Sortir
The integer in string is : 20 The float in string is : 30.500000>
Complexité temporelle : Sur)
Espace auxiliaire : Sur)
Méthode 2 : utilisation de flux de chaînes
Dans cette méthode, un flux de chaîne déclare un objet flux qui insère d'abord un nombre, sous forme de flux dans un objet, puis utilise str() pour suivre la conversion interne d'un nombre en chaîne.
Exemple:
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;> }> |
>
>Sortir
The newly formed string from number is : 2016>
Complexité temporelle : Sur)
Espace auxiliaire : Sur)
Méthode 3 : Utilisation de la fonction sprintf()
sprintf() La fonction stocke la sortie sur le tampon de caractères spécifié dans la fonction, au lieu d'imprimer la sortie sur la console.
C++
si instruction sinon java
// 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> |
>
>Sortir
the string is : 12234>
Complexité temporelle : Sur)
Espace auxiliaire : Sur)
Méthode 4 : Utiliser la distribution lexicale boostée
Semblable à la conversion de chaîne, la fonction lexical_cast() reste la même, mais dans le ' renforcer la distribution lexicale ' La liste d'arguments temporels est modifiée en lexical_cast (numeric_var).
Exemple:
C++
// 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;> }> |
>
modèles de conception Java
>Sortir
The float value in string is : 10.5 The int value in string is : 17>
Complexité temporelle : Sur)
Espace auxiliaire : Sur)