logo

Pretvorite niz u cijeli broj u C++

U ovom odjeljku raspravljat će se o različitim metodama pretvaranja zadanih podataka niza u cijeli broj pomoću programskog jezika C++. Postoje neke situacije ili instance u kojima trebamo pretvoriti određene podatke u drugu vrstu, a jedna od takvih situacija je pretvaranje niza u int podatke u programiranju.

Na primjer, imamo numerički niz kao ' 143 ', a mi ga želimo pretvoriti u numerički tip. Trebamo upotrijebiti funkciju koja pretvara niz u cijeli broj i vraća numeričke podatke kao 143. Sada ćemo naučiti svaku metodu koja pomaže pretvoriti niz podataka u cijele brojeve u programskom jeziku C++.

Pretvorite niz u cijeli broj u C++

Različite metode za pretvaranje string podataka u cijele brojeve u C++ programskom jeziku.

  1. Korištenje klase stringstream
  2. Korištenje funkcije stoi().
  3. Korištenje funkcije atoi().
  4. Korištenje funkcije sscanf().

Korištenje klase stringstream

The stringstream je klasa koja se koristi za pretvaranje numeričkog niza u int tip. Klasa stringstream deklarira objekt toka kako bi umetnula niz kao objekt toka, a zatim ekstrahira konvertirane podatke cijelog broja na temelju tokova. Klasa stringstream ima '<>' operatore koji se koriste za dohvaćanje podataka iz (<>) lijevog operatora.

Kreirajmo program za demonstraciju klase stringstream za pretvaranje podataka niza u cijeli broj u programskom jeziku C++.

null provjera u Javi

Program1.cpp

 #include #include // use stringstream class using namespace std; int main() { string str1 = &apos;143&apos;; // declare a string int intdata; // declare integer variable /* use stringstream class to declare a stream object to insert a string and then fetch as integer type data. */ stringstream obj; obj &lt;&gt; intdata; // fetch integer type data cout &lt;&lt; &apos; The string value is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The representation of the string to integer type data is: &apos; &lt;&lt; intdata &lt;&lt; endl; return 0; } 

Izlaz

 The string value is: 143 The representation of the string to integer type data is: 143 

U gornjem programu koristimo klasu stringstream za stvaranje objekta obj, a ona pomaže pretvoriti podatke niza u cijeli broj. Zatim koristimo '<>' operator za izdvajanje pretvorenog niza iz obj u numeričke podatke.

Korištenje funkcije sscanf().

Funkcija sscanf() pretvara dati niz u određeni tip podataka poput cijelog broja.

Sintaksa

kako pozvati metodu u Javi
 sccanf ( str, %d, &amp;intvar); 

Funkcija sscanf() ima tri argumenta za određivanje znakovnog niza (str), specifikatora podataka (%d) i cjelobrojne varijable (&intvar) za pohranjivanje pretvorenog niza.

Algoritam funkcije sscanf().

  1. Funkcija sscanf() pripada klasi stringstream, pa moramo uvesti klasu u naš program.
  2. Inicijalizirati konstantni znakovni niz str.
  3. Stvorite cjelobrojnu varijablu za držanje pretvorenog niza u cjelobrojne vrijednosti.
  4. Proslijedite varijablu niza u funkciju sscanf() i dodijelite funkciju sscanf() varijabli cijelog broja za pohranjivanje vrijednosti cijelog broja koju je generirala funkcija.
  5. Ispišite cjelobrojne vrijednosti.

Razmotrimo primjer korištenja funkcije sscanf() za pretvaranje niza u numerički broj u C++.

Program2.cpp

 #include #include // use stringstream class using namespace std; int main () { // declare the character strings const char *str1 = &apos;555&apos;; const char *str2 = &apos;143&apos;; const char *str3 = &apos;101&apos;; // declare the integer variables int numdata1; int numdata2; int numdata3; /* use sscanf() function and to pass the character string str1, and an integer variable to hold the string. */ sscanf (str1, &apos;%d&apos;, &amp;numdata1); cout &lt;<' the value of character string is: ' << str1; cout '
 representation to int numdata1 <<endl; sscanf (str2, '%d', &numdata2); <<'
 str2; numdata2 (str3, &numdata3); str3; numdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The value of the character string is: 555 The representation of string to int value of numdata is: 555 The value of the character string is: 143 The representation of string to int value of numdata is: 143 The value of the character string is: 101 The representation of string to int value of numdata is: 101 </pre> <h3>Using the stoi() function</h3> <p>The stoi() function converts a string data to an integer type by passing the string as a parameter to return an integer value.</p> <p> <strong>Syntax</strong> </p> <pre> stoi(str); </pre> <p>The stoi() function contains an str argument. The str string is passed inside the stoi() function to convert string data into an integer value.</p> <p> <strong>Algorithm of the stoi() function</strong> </p> <ol class="points"> <li>Initialize the string variable to store string values.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the stoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the stoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include using namespace std; int main () { string strdata1 = &apos;108&apos;; string strdata2 = &apos;56.78&apos;; string strdata3 = &apos;578 Welcome&apos;; // use stoi() function to pass string as arguments int intdata1 = stoi (strdata1); int intdata2 = stoi (strdata2); int intdata3 = stoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer using stoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << ' the conversion of string to an integer using stoi(\'' strdata2 '\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer using stoi(&apos;108&apos;) is 108 The conversion of string to an integer using stoi(&apos;56.78&apos;) is 56 The conversion of string to an integer using stoi(&apos;578 Welcome&apos;) is 578 </pre> <h3>Using the atoi() function</h3> <p>An atoi() function is used to convert the character string into an integer value. An atoi() function passes the character type string to return the integer data.</p> <p> <strong>Syntax</strong> </p> <pre> atoi (const char *str); </pre> <p> <strong>Algorithm of the atoi() function</strong> </p> <ol class="points"> <li>Initialize a pointer-type character array to store a string.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the atoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << ' the conversion of string to an integer value using atoi(\'' strdata2 '\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;></pre></endl;></pre></'>

Korištenje funkcije stoi().

Funkcija stoi() pretvara niz podataka u cjelobrojni tip prosljeđivanjem niza kao parametra za vraćanje cjelobrojne vrijednosti.

Sintaksa

 stoi(str); 

Funkcija stoi() sadrži str argument. Niz str prosljeđuje se unutar funkcije stoi() za pretvaranje podataka niza u cjelobrojnu vrijednost.

Algoritam funkcije stoi().

  1. Inicijalizirajte varijablu niza za pohranu vrijednosti niza.
  2. Nakon toga stvara varijablu integer tipa koja pohranjuje pretvorbu niza u podatke integer tipa pomoću funkcije stoi().
  3. Ispiši vrijednost cjelobrojne varijable.

Kreirajmo program za korištenje funkcije stoi() za pretvaranje vrijednosti niza u cjelobrojni tip u programskom jeziku C++.

Program3.cpp

 #include #include using namespace std; int main () { string strdata1 = &apos;108&apos;; string strdata2 = &apos;56.78&apos;; string strdata3 = &apos;578 Welcome&apos;; // use stoi() function to pass string as arguments int intdata1 = stoi (strdata1); int intdata2 = stoi (strdata2); int intdata3 = stoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer using stoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer using stoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer using stoi(&apos;108&apos;) is 108 The conversion of string to an integer using stoi(&apos;56.78&apos;) is 56 The conversion of string to an integer using stoi(&apos;578 Welcome&apos;) is 578 </pre> <h3>Using the atoi() function</h3> <p>An atoi() function is used to convert the character string into an integer value. An atoi() function passes the character type string to return the integer data.</p> <p> <strong>Syntax</strong> </p> <pre> atoi (const char *str); </pre> <p> <strong>Algorithm of the atoi() function</strong> </p> <ol class="points"> <li>Initialize a pointer-type character array to store a string.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the atoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer value using atoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;></pre></endl;>

Korištenje funkcije atoi().

Funkcija atoi() koristi se za pretvaranje niza znakova u cjelobrojnu vrijednost. Funkcija atoi() prosljeđuje znakovni niz da bi vratila podatke cijelog broja.

isprobajte strukturu podataka

Sintaksa

 atoi (const char *str); 

Algoritam funkcije atoi().

  1. Inicijalizirajte niz znakova tipa pokazivača za pohranjivanje niza.
  2. Nakon toga stvara varijablu integer tipa koja pohranjuje pretvorbu niza u podatke integer tipa pomoću funkcije atoi().
  3. Ispiši vrijednost cjelobrojne varijable.

Kreirajmo program za korištenje funkcije atoi() za pretvaranje vrijednosti niza u cjelobrojni tip u programskom jeziku C++.

Program4.cpp

 #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer value using atoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;>