logo

Konverter streng til heltall i C++

Denne delen vil diskutere de forskjellige metodene for å konvertere de gitte strengdataene til et heltall ved å bruke C++-programmeringsspråket. Det er noen situasjoner eller tilfeller der vi trenger å konvertere en bestemt data til en annen type, og en slik situasjon er å konvertere streng til int-data i programmering.

For eksempel har vi en numerisk streng som ' 143 ', og vi ønsker å konvertere den til en numerisk type. Vi må bruke en funksjon som konverterer en streng til et heltall og returnerer de numeriske dataene som 143. Nå skal vi lære hver metode som hjelper til med å konvertere strengdata til heltall i programmeringsspråket C++.

Konverter streng til heltall i C++

Ulike metoder for å konvertere strengdata til heltall i programmeringsspråket C++.

  1. Bruke stringstream-klassen
  2. Bruker funksjonen stoi().
  3. Bruke atoi()-funksjonen
  4. Bruke sscanf()-funksjonen

Bruke stringstream-klassen

De strengstrøm er en klasse som brukes til å konvertere en numerisk streng til en int-type. Stringstream-klassen erklærer at et strømobjekt skal sette inn en streng som et strømobjekt og trekker deretter ut de konverterte heltallsdataene basert på strømmene. Stringstream-klassen har '<>'-operatorer, som brukes til å hente data fra (<>) venstre operator.

La oss lage et program for å demonstrere stringstream-klassen for å konvertere strengdataene til et heltall i C++-programmeringsspråket.

all caps kommandoen excel

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

Produksjon

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

I programmet ovenfor bruker vi stringstream-klassen til å lage et obj-objekt, og det hjelper å konvertere strengdata til et heltall. Vi bruker deretter '<>'-operatoren for å trekke ut den konverterte strengen fra obj til numeriske data.

Bruke sscanf()-funksjonen

En sscanf()-funksjon konverterer en gitt streng til en spesifisert datatype som et heltall.

strengdeling c++

Syntaks

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

sscanf()-funksjonen har tre argumenter for å spesifisere char-strengen (str), dataspesifikatoren (%d) og heltallsvariabelen (&intvar) for å lagre den konverterte strengen.

Algoritme til sscanf()-funksjonen

  1. Funksjonen sscanf() tilhører stringstream-klassen, så vi må importere klassen til programmet vårt.
  2. Initialiser en konstant tegnstreng str.
  3. Opprett en heltallsvariabel for å holde den konverterte strengen til heltallsverdier.
  4. Send strengvariabelen inn i sscanf()-funksjonen, og tilordne sscanf()-funksjonen til heltallsvariabelen for å lagre heltallsverdien generert av funksjonen.
  5. Skriv ut heltallsverdiene.

La oss vurdere et eksempel for å bruke funksjonen sscanf() for å konvertere strengen til numerisk tall i 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></'>

Bruker funksjonen stoi().

Stoi()-funksjonen konverterer en strengdata til en heltallstype ved å sende strengen som en parameter for å returnere en heltallsverdi.

Syntaks

 stoi(str); 

Stoi()-funksjonen inneholder et str-argument. Str-strengen sendes inne i stoi()-funksjonen for å konvertere strengdata til en heltallsverdi.

Algoritme for stoi()-funksjonen

java oppslag
  1. Initialiser strengvariabelen for å lagre strengverdier.
  2. Etter det oppretter den en heltallstypevariabel som lagrer konverteringen av streng til heltallstypedata ved å bruke stoi()-funksjonen.
  3. Skriv ut heltallsvariabelverdien.

La oss lage et program for å bruke stoi()-funksjonen til å konvertere strengverdien til heltallstypen 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;>

Bruke atoi()-funksjonen

En atoi()-funksjon brukes til å konvertere tegnstrengen til en heltallsverdi. En atoi()-funksjon sender tegntypestrengen for å returnere heltallsdataene.

Syntaks

 atoi (const char *str); 

Algoritme for atoi()-funksjonen

  1. Initialiser en tegnarray av pekertype for å lagre en streng.
  2. Etter det oppretter den en heltallstypevariabel som lagrer konverteringen av streng til heltallstypedata ved å bruke atoi()-funksjonen.
  3. Skriv ut heltallsvariabelverdien.

La oss lage et program for å bruke atoi()-funksjonen til å konvertere strengverdien til heltallstypen i C++-programmeringsspråket.

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