logo

Converteix cadena a enter en C++

En aquesta secció es discutiran els diferents mètodes per convertir les dades de cadena donades en un nombre enter mitjançant el llenguatge de programació C++. Hi ha algunes situacions o casos en què hem de convertir certes dades a un altre tipus, i una d'aquestes situacions és convertir string en dades int en programació.

Per exemple, tenim una cadena numèrica com ' 143 ', i volem convertir-lo en un tipus numèric. Hem d'utilitzar una funció que converteixi una cadena en un enter i retorni les dades numèriques com a 143. Ara, aprendrem cada mètode que ajudi a convertir les dades de la cadena en nombres enters en el llenguatge de programació C++.

Converteix cadena a enter en C++

Diferents mètodes per convertir les dades de la cadena en nombres enters en el llenguatge de programació C++.

  1. Utilitzant la classe stringstream
  2. Utilitzant la funció stoi().
  3. Utilitzant la funció atoi().
  4. Utilitzant la funció sscanf().

Utilitzant la classe stringstream

El corrent de corda és una classe utilitzada per convertir una cadena numèrica a un tipus int. La classe stringstream declara un objecte de flux per inserir una cadena com a objecte de flux i després extreu les dades senceres convertides en funció dels fluxos. La classe stringstream té operadors '<>', que s'utilitzen per obtenir dades de l'operador esquerre (<>).

Creem un programa per demostrar la classe stringstream per convertir les dades de string en un nombre enter en el llenguatge de programació C++.

cadena a la data

Programa 1.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; } 

Sortida

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

Al programa anterior, utilitzem la classe stringstream per crear un objecte obj i ajuda a convertir les dades de cadena en un nombre enter. A continuació, utilitzem l'operador '<>' per extreure la cadena convertida d'obj en dades numèriques.

Utilitzant la funció sscanf().

Una funció sscanf() converteix una cadena determinada en un tipus de dades especificat com un nombre enter.

Sintaxi

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

La funció sscanf() té tres arguments per especificar la cadena de caràcters (str), l'especificador de dades (%d) i la variable entera (&intvar) per emmagatzemar la cadena convertida.

canyella vs mate

Algorisme de la funció sscanf().

  1. La funció sscanf() pertany a la classe stringstream, de manera que hem d'importar la classe al nostre programa.
  2. Inicialitzar una cadena de caràcters constant str.
  3. Creeu una variable entera per mantenir la cadena convertida en valors enters.
  4. Passeu la variable de cadena a la funció sscanf() i assigneu la funció sscanf() a la variable entera per emmagatzemar el valor sencer generat per la funció.
  5. Imprimeix els valors enters.

Considerem un exemple per utilitzar la funció sscanf() per convertir la cadena en nombre numèric en C++.

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

Utilitzant la funció stoi().

La funció stoi() converteix les dades d'una cadena en un tipus enter passant la cadena com a paràmetre per retornar un valor enter.

Sintaxi

 stoi(str); 

La funció stoi() conté un argument str. La cadena str es passa dins de la funció stoi() per convertir les dades de la cadena en un valor enter.

Algorisme de la funció stoi().

  1. Inicialitzeu la variable de cadena per emmagatzemar valors de cadena.
  2. Després d'això, crea una variable de tipus enter que emmagatzema la conversió de la cadena en dades de tipus enter mitjançant la funció stoi().
  3. Imprimeix el valor de la variable enter.

Creem un programa per utilitzar la funció stoi() per convertir el valor de la cadena en el tipus enter en el llenguatge de programació C++.

Programa 3.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;>

Utilitzant la funció atoi().

S'utilitza una funció atoi() per convertir la cadena de caràcters en un valor enter. Una funció atoi() passa la cadena de tipus de caràcter per retornar les dades senceres.

Sintaxi

 atoi (const char *str); 

Algorisme de la funció atoi().

  1. Inicialitzeu una matriu de caràcters de tipus punter per emmagatzemar una cadena.
  2. Després d'això, crea una variable de tipus enter que emmagatzema la conversió de la cadena en dades de tipus enter mitjançant la funció atoi().
  3. Imprimeix el valor de la variable enter.

Creem un programa per utilitzar la funció atoi() per convertir el valor de la cadena en el tipus enter en el llenguatge de programació C++.

alinear la imatge CSS

Programa 4.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;>