Pretvarjanje števila v niz v C++
V C++ je pretvorba celih števil v nize ali pretvorba števil v nize ali obratno pravzaprav sama po sebi velika sprememba paradigme. Na splošno ali natančneje v tekmovalnem programiranju je veliko primerov, ko moramo število pretvoriti v niz ali niz v število. Oglejmo si nekaj metod za pretvorbo celega števila ali števila v niz.
Pretvarjanje števila v niz v C++
obstajajo 4 glavne metode za pretvorbo števila v niz , ki so naslednji:
- Uporaba to_string() Uporaba string Stream Uporaba funkcije sprintf() Uporaba boost lexical cast
1. način: uporaba to_string()
The to_string() funkcijo lahko uporabite za pretvorbo celega števila, vrednosti s plavajočo vejico ali poljubnega števila v niz. Ta funkcija sprejme število (ki je lahko katera koli vrsta podatkov) in vrne število kot želeni niz.
Sintaksa :
string to_string (int val );
Parametri:
- val – Katera koli številska vrednost.
Povratna vrednost:
- Objekt niza, ki vsebuje predstavitev vrednosti kot zaporedje znakov.
Primer :
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;> }> |
Izhod
The integer in string is : 20 The float in string is : 30.500000
Časovna zapletenost: O(n)
Pomožni prostor: O(n)
2. način: Uporaba tokov nizov
Pri tej metodi tok niza deklarira objekt toka, ki najprej vstavi številko kot tok v objekt in nato uporabi str() sledi notranji pretvorbi števila v niz.
primer:
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;> }> |
Izhod
The newly formed string from number is : 2016
Časovna zapletenost: O(n)
Pomožni prostor: O(n)
3. način: Uporaba funkcije sprintf().
sprintf() funkcija shrani izhod v medpomnilnik char, določen v funkciji, namesto da bi natisnil izhod na konzoli.
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> |
Izhod
the string is : 12234
Časovna zapletenost: O(n)
Pomožni prostor: O(n)
4. način: Uporaba povečane leksikalne zasedbe
Podobno kot pri pretvorbi nizov ostaja funkcija lexical_cast() enaka, vendar v povečati leksikalno zasedbo ' časovni seznam argumentov se spremeni v lexical_cast(numeric_var).
primer:
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;> }> |
Izhod
The float value in string is : 10.5 The int value in string is : 17
Časovna zapletenost : O(n)
Pomožni prostor : O(n)