logo

Converti una stringa in un numero intero in C++

Questa sezione discuterà i diversi metodi per convertire i dati di stringa specificati in un numero intero utilizzando il linguaggio di programmazione C++. Esistono alcune situazioni o istanze in cui è necessario convertire determinati dati in un altro tipo e una di queste situazioni è convertire una stringa in dati interi nella programmazione.

Ad esempio, abbiamo una stringa numerica come ' 143 ', e vogliamo convertirlo in un tipo numerico. Dobbiamo utilizzare una funzione che converta una stringa in un numero intero e restituisca i dati numerici come 143. Ora impareremo ogni metodo che aiuta a convertire i dati stringa in numeri interi nel linguaggio di programmazione C++.

Converti una stringa in un numero intero in C++

Diversi metodi per convertire i dati della stringa in numeri interi nel linguaggio di programmazione C++.

  1. Utilizzando la classe stringstream
  2. Utilizzando la funzione stoi()
  3. Utilizzando la funzione atoi()
  4. Utilizzando la funzione sscanf()

Utilizzando la classe stringstream

IL stringstream è una classe utilizzata per convertire una stringa numerica in un tipo int. La classe stringstream dichiara un oggetto stream per inserire una stringa come oggetto stream e quindi estrae i dati interi convertiti in base ai flussi. La classe stringstream ha operatori '<>', che vengono utilizzati per recuperare i dati dall'operatore sinistro (<>).

Creiamo un programma per dimostrare la classe stringstream per convertire i dati della stringa in un numero intero nel linguaggio di programmazione C++.

pulsante centrale nei css

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

Produzione

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

Nel programma sopra, utilizziamo la classe stringstream per creare un oggetto obj e aiuta a convertire i dati della stringa in un numero intero. Utilizziamo quindi l'operatore '<>' per estrarre la stringa convertita da obj in dati numerici.

Utilizzando la funzione sscanf()

Una funzione sscanf() converte una determinata stringa in un tipo di dati specificato come un numero intero.

Sintassi

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

La funzione sscanf() dispone di tre argomenti per specificare la stringa di caratteri (str), l'identificatore di dati (%d) e la variabile intera (&intvar) per archiviare la stringa convertita.

Algoritmo della funzione sscanf()

  1. La funzione sscanf() appartiene alla classe stringstream, quindi dobbiamo importare la classe nel nostro programma.
  2. Inizializza una stringa di caratteri costante str.
  3. Crea una variabile intera per contenere la stringa convertita in valori interi.
  4. Passare la variabile stringa nella funzione sscanf() e assegnare la funzione sscanf() alla variabile intera per memorizzare il valore intero generato dalla funzione.
  5. Stampa i valori interi.

Consideriamo un esempio per utilizzare la funzione sscanf() per convertire la stringa in un numero numerico in C++.

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

Utilizzando la funzione stoi()

La funzione stoi() converte i dati di una stringa in un tipo intero passando la stringa come parametro per restituire un valore intero.

python stampa a 2 cifre decimali

Sintassi

 stoi(str); 

La funzione stoi() contiene un argomento str. La stringa str viene passata all'interno della funzione stoi() per convertire i dati della stringa in un valore intero.

Algoritmo della funzione stoi()

  1. Inizializza la variabile stringa per memorizzare valori stringa.
  2. Successivamente, crea una variabile di tipo intero che memorizza la conversione della stringa in dati di tipo intero utilizzando la funzione stoi().
  3. Stampa il valore della variabile intera.

Creiamo un programma per utilizzare la funzione stoi() per convertire il valore stringa nel tipo intero nel linguaggio di programmazione C++.

Programma3.cpp

c++ converte int in stringa
 #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;>

Utilizzando la funzione atoi()

Una funzione atoi() viene utilizzata per convertire la stringa di caratteri in un valore intero. Una funzione atoi() passa la stringa del tipo di carattere per restituire i dati interi.

Sintassi

 atoi (const char *str); 

Algoritmo della funzione atoi()

  1. Inizializza una matrice di caratteri di tipo puntatore per memorizzare una stringa.
  2. Successivamente, crea una variabile di tipo intero che memorizza la conversione della stringa in dati di tipo intero utilizzando la funzione atoi().
  3. Stampa il valore della variabile intera.

Creiamo un programma per utilizzare la funzione atoi() per convertire il valore stringa nel tipo intero nel linguaggio di programmazione 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;>