logo

Converteer tekenreeks naar geheel getal in C++

In deze sectie worden de verschillende methoden besproken om de gegeven stringgegevens naar een geheel getal te converteren met behulp van de programmeertaal C++. Er zijn enkele situaties of gevallen waarin we bepaalde gegevens naar een ander type moeten converteren, en een van die situaties is het converteren van string naar int-gegevens tijdens het programmeren.

We hebben bijvoorbeeld een numerieke reeks als ' 143 ', en we willen het omzetten naar een numeriek type. We moeten een functie gebruiken die een string omzet in een geheel getal en de numerieke gegevens retourneert als 143. Nu zullen we elke methode leren die helpt bij het omzetten van stringgegevens in gehele getallen in de programmeertaal C++.

Converteer tekenreeks naar geheel getal in C++

Verschillende methoden om de stringgegevens om te zetten in gehele getallen in de programmeertaal C++.

  1. De stringstream-klasse gebruiken
  2. De functie stoi() gebruiken
  3. De functie atoi() gebruiken
  4. De functie sscanf() gebruiken

De stringstream-klasse gebruiken

De stringstroom is een klasse die wordt gebruikt om een ​​numerieke string naar een int-type te converteren. De stringstream-klasse declareert een stream-object om een ​​string in te voegen als een stream-object en extraheert vervolgens de geconverteerde gehele gegevens op basis van de streams. De stringstream-klasse heeft '<>'-operatoren, die worden gebruikt om gegevens op te halen van de (<>) linkeroperator.

Laten we een programma maken om de stringstream-klasse te demonstreren voor het converteren van de stringgegevens naar een geheel getal in de programmeertaal C++.

hoe u de schermgrootte kunt controleren

Programma1.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; } 

Uitvoer

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

In het bovenstaande programma gebruiken we de stringstream-klasse om een ​​obj-object te maken, en het helpt om stringgegevens naar een geheel getal te converteren. Vervolgens gebruiken we de operator '<>' om de geconverteerde tekenreeks uit obj naar numerieke gegevens te extraheren.

De functie sscanf() gebruiken

Een sscanf()-functie converteert een gegeven string naar een gespecificeerd gegevenstype, zoals een geheel getal.

gelinkte lijst java

Syntaxis

 sccanf ( str, %d, &amp;intvar); 

De functie sscanf() heeft drie argumenten om de char string (str), de gegevensspecificatie (%d) en de integer-variabele (&intvar) op te geven om de geconverteerde string op te slaan.

Algoritme van de functie sscanf().

  1. De functie sscanf() behoort tot de klasse stringstream, dus we moeten de klasse in ons programma importeren.
  2. Initialiseer een constante tekenreeks str.
  3. Maak een geheel getalvariabele om de geconverteerde tekenreeks vast te houden aan geheeltallige waarden.
  4. Geef de stringvariabele door aan de functie sscanf() en wijs de functie sscanf() toe aan de integer-variabele om de gehele waarde op te slaan die door de functie wordt gegenereerd.
  5. Druk de gehele waarden af.

Laten we een voorbeeld bekijken waarin we de functie sscanf() gebruiken om de tekenreeks naar een numeriek getal in C++ te converteren.

Programma2.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></'>

De functie stoi() gebruiken

De functie stoi() converteert tekenreeksgegevens naar een geheel getal door de tekenreeks als parameter door te geven om een ​​geheel getal terug te geven.

Syntaxis

 stoi(str); 

De functie stoi() bevat een str-argument. De str-tekenreeks wordt doorgegeven binnen de stoi()-functie om tekenreeksgegevens om te zetten in een geheel getal.

Algoritme van de functie stoi().

'kruskal's algoritme'
  1. Initialiseer de stringvariabele om stringwaarden op te slaan.
  2. Daarna creëert het een variabele van het type integer die de conversie van string naar gegevens van het integer-type opslaat met behulp van de functie stoi().
  3. Druk de gehele variabelewaarde af.

Laten we een programma maken dat de functie stoi() gebruikt om de tekenreekswaarde om te zetten in het gehele type in de programmeertaal C++.

Programma3.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;>

De functie atoi() gebruiken

Een atoi()-functie wordt gebruikt om de tekenreeks om te zetten in een geheel getal. Een atoi()-functie geeft de tekenreeks door om de gehele gegevens terug te geven.

Syntaxis

 atoi (const char *str); 

Algoritme van de functie atoi().

  1. Initialiseer een pointer-type tekenarray om een ​​tekenreeks op te slaan.
  2. Daarna creëert het een variabele van het type integer die de conversie van string naar gegevens van het integer-type opslaat met behulp van de functie atoi().
  3. Druk de gehele variabelewaarde af.

Laten we een programma maken dat de functie atoi() gebruikt om de tekenreekswaarde om te zetten in het gehele type in de programmeertaal C++.

Programma4.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;>