V C++ je prevod celých čísel na reťazce alebo prevod čísel na reťazce alebo naopak v skutočnosti sám o sebe veľkým posunom paradigmy. Vo všeobecnosti alebo konkrétnejšie v konkurenčnom programovaní existuje veľa prípadov, keď potrebujeme previesť číslo na reťazec alebo reťazec na číslo. Pozrime sa na niektoré metódy prevodu celého čísla alebo čísla na reťazec.
Prevod čísla na reťazec v C++
Existujú 4 hlavné metódy na prevod čísla na reťazec , ktoré sú nasledovné:
- Použitie to_string() Použitie reťazca Stream Použitie funkcie sprintf() Použitie boost lexical cast
Metóda 1: Použitie to_string()
The natiahnuť() funkciu možno použiť na prevod celého čísla, hodnôt s pohyblivou rádovou čiarkou alebo akéhokoľvek čísla na reťazec. Táto funkcia akceptuje číslo (ktoré môže byť ľubovoľného dátového typu) a vráti číslo ako požadovaný reťazec.
Syntax :
string to_string (int val );>
Parametre:
- val – Akákoľvek číselná hodnota.
Návratová hodnota:
- Reťazový objekt obsahujúci reprezentáciu hodnoty ako postupnosť znakov.
Príklad :
C++
stredové tlačidlo css
// 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;> }> |
>
jtlačidlo
>Výkon
The integer in string is : 20 The float in string is : 30.500000>
Časová zložitosť: O(n)
Pomocný priestor: O(n)
Metóda 2: Použitie reťazcov
V tejto metóde prúd reťazca deklaruje objekt prúdu, ktorý najprv vloží číslo ako prúd do objektu a potom použije str() nasledovať interný prevod čísla na reťazec.
Príklad:
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;> }> |
>
>Výkon
The newly formed string from number is : 2016>
Časová zložitosť: O(n)
Pomocný priestor: O(n)
Metóda 3: Použitie funkcie sprintf().
sprintf() funkcia ukladá výstup do vyrovnávacej pamäte znakov špecifikovanej vo funkcii namiesto tlače výstupu na konzole.
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> |
>
blokovanie reklám na youtube pre Android
>Výkon
the string is : 12234>
Časová zložitosť: O(n)
Pomocný priestor: O(n)
Metóda 4: Použitie zosilnenia lexikálneho obsadzovania
Podobne ako pri konverzii reťazcov, funkcia lexical_cast() zostáva rovnaká, ale v „ posilniť lexikálne obsadenie “ zoznam časových argumentov sa upraví na lexical_cast(numeric_var).
podreťazec java obsahuje
Príklad:
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;> }> |
>
>Výkon
The float value in string is : 10.5 The int value in string is : 17>
Časová zložitosť : O(n)
Pomocný priestor : O(n)