logo

Konvertera sträng till heltal i C++

Det här avsnittet kommer att diskutera de olika metoderna för att konvertera givna strängdata till ett heltal med hjälp av programmeringsspråket C++. Det finns vissa situationer eller fall där vi behöver konvertera en viss data till en annan typ, och en sådan situation är att konvertera sträng till int-data i programmering.

Till exempel har vi en numerisk sträng som ' 143 ', och vi vill konvertera den till en numerisk typ. Vi måste använda en funktion som konverterar en sträng till ett heltal och returnerar numeriska data som 143. Nu ska vi lära oss varje metod som hjälper till att konvertera strängdata till heltal i programmeringsspråket C++.

Konvertera sträng till heltal i C++

Olika metoder för att konvertera strängdata till heltal i programmeringsspråket C++.

  1. Använder klassen stringstream
  2. Använda stoi()-funktionen
  3. Använder funktionen atoi().
  4. Använda funktionen sscanf().

Använder klassen stringstream

De strängström är en klass som används för att konvertera en numerisk sträng till en int-typ. Klassen stringstream deklarerar ett strömobjekt för att infoga en sträng som ett strömobjekt och extraherar sedan den konverterade heltalsdatan baserat på strömmarna. Stringstream-klassen har '<>'-operatorer, som används för att hämta data från (<>) vänster operator.

Låt oss skapa ett program för att demonstrera stringstream-klassen för att konvertera strängdata till ett heltal i programmeringsspråket C++.

bool till sträng java

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

Produktion

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

I programmet ovan använder vi klassen stringstream för att skapa ett obj-objekt, och det hjälper till att konvertera strängdata till ett heltal. Vi använder sedan operatorn '<>' för att extrahera den konverterade strängen från obj till numerisk data.

Använda funktionen sscanf().

En sscanf()-funktion omvandlar en given sträng till en specificerad datatyp som ett heltal.

Syntax

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

Funktionen sscanf() har tre argument för att specificera char-strängen (str), dataspecifikatorn (%d) och heltalsvariabeln (&intvar) för att lagra den konverterade strängen.

Algoritm för sscanf()-funktionen

  1. Funktionen sscanf() tillhör klassen stringstream, så vi måste importera klassen till vårt program.
  2. Initiera en konstant teckensträng str.
  3. Skapa en heltalsvariabel för att hålla den konverterade strängen till heltalsvärden.
  4. Skicka strängvariabeln till funktionen sscanf() och tilldela funktionen sscanf() till heltalsvariabeln för att lagra heltalsvärdet som genereras av funktionen.
  5. Skriv ut heltalsvärdena.

Låt oss överväga ett exempel för att använda funktionen sscanf() för att konvertera strängen till ett numeriskt tal i C++.

25 c till k

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

Använda stoi()-funktionen

Funktionen stoi() konverterar en strängdata till en heltalstyp genom att skicka strängen som en parameter för att returnera ett heltalsvärde.

Syntax

 stoi(str); 

Funktionen stoi() innehåller ett str-argument. Str-strängen skickas in i stoi()-funktionen för att konvertera strängdata till ett heltalsvärde.

Algoritm för stoi()-funktionen

  1. Initiera strängvariabeln för att lagra strängvärden.
  2. Efter det skapar den en heltalstypvariabel som lagrar omvandlingen av sträng till data av heltalstyp med hjälp av stoi()-funktionen.
  3. Skriv ut heltalsvariabelns värde.

Låt oss skapa ett program för att använda stoi()-funktionen för att konvertera strängvärdet till heltalstypen i programmeringsspråket 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;>

Använder funktionen atoi().

En atoi()-funktion används för att konvertera teckensträngen till ett heltalsvärde. En atoi()-funktion skickar teckentypsträngen för att returnera heltalsdata.

Syntax

heapify sortera
 atoi (const char *str); 

Algoritm för atoi()-funktionen

  1. Initiera en teckenuppsättning av pekare för att lagra en sträng.
  2. Efter det skapar den en heltalstypvariabel som lagrar omvandlingen av sträng till data av heltalstyp med hjälp av atoi()-funktionen.
  3. Skriv ut heltalsvariabelns värde.

Låt oss skapa ett program för att använda funktionen atoi() för att konvertera strängvärdet till heltalstypen i programmeringsspråket 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;>