In C++ is het converteren van gehele getallen naar strings of het converteren van getallen naar strings of omgekeerd eigenlijk een grote paradigmaverschuiving op zichzelf. In het algemeen of meer specifiek zijn er bij competitief programmeren veel gevallen waarin we een getal naar een string of string naar een getal moeten converteren. Laten we eens kijken naar enkele methoden om een geheel getal of getal naar een tekenreeks te converteren.
Getal naar tekenreeks converteren in C++
Er zijn 4 belangrijke methoden om een getal naar een string te converteren , die als volgt zijn:
- Gebruik to_string() Gebruik string Stream Gebruik sprintf() functie Gebruik boost lexicale cast
Methode 1: to_string() gebruiken
De to_string() functie kan worden gebruikt om een geheel getal, waarden met drijvende komma of een willekeurig getal naar een tekenreeks te converteren. Deze functie accepteert een getal (dit kan elk gegevenstype zijn) en retourneert het getal als de gewenste tekenreeks.
Syntaxis :
string to_string (int val );>
Parameters:
- val – Elke numerieke waarde.
Winstwaarde:
- Een stringobject dat de representatie van waarde bevat als een reeks tekens.
Voorbeeld :
C++
selectie html
// 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;> }> |
>
>Uitvoer
The integer in string is : 20 The float in string is : 30.500000>
Tijdcomplexiteit: Op)
Hulpruimte: Op)
fibonacci-code java
Methode 2: Stringstreams gebruiken
Bij deze methode declareert een stringstream een streamobject dat eerst een getal invoegt, als een stream in een object en vervolgens gebruikt str() om de interne conversie van een getal naar een string te volgen.
Voorbeeld:
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;> }> |
>
np.betekenen
>Uitvoer
The newly formed string from number is : 2016>
Tijdcomplexiteit: Op)
Hulpruimte: Op)
Methode 3: De functie sprintf() gebruiken
sprintf() function slaat de uitvoer op in de char-buffer die is opgegeven in de functie, in plaats van de uitvoer op de console af te drukken.
C++
ontwerppatronen in 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> |
>
>Uitvoer
the string is : 12234>
Tijdcomplexiteit: Op)
Hulpruimte: Op)
Methode 4: Gebruik van boost-lexicale cast
Net als bij tekenreeksconversie blijft de functie lexical_cast() hetzelfde, maar in de ‘ lexicale cast versterken ‘ tijdargumentlijst wordt gewijzigd in lexical_cast (numerieke_var).
Voorbeeld:
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;> }> |
>
css vetgedrukt
>Uitvoer
The float value in string is : 10.5 The int value in string is : 17>
Tijdcomplexiteit : Op)
Hulpruimte : Op)