logo

Dizeyi C++'da tam sayıya dönüştürün

Bu bölümde, C++ programlama dilini kullanarak verilen dize verilerini bir tamsayıya dönüştürmenin farklı yöntemleri tartışılacaktır. Belirli bir veriyi başka bir türe dönüştürmemiz gereken bazı durumlar veya örnekler vardır ve bu tür durumlardan biri, programlamada string'i int veriye dönüştürmektir.

Örneğin, ' şeklinde bir sayısal dizimiz var. 143 ' ve onu sayısal bir türe dönüştürmek istiyoruz. Bir stringi tamsayıya dönüştüren ve sayısal veriyi 143 olarak döndüren bir fonksiyon kullanmamız gerekiyor. Şimdi C++ programlama dilinde string veriyi tamsayıya dönüştürmeye yardımcı olan her yöntemi öğreneceğiz.

Dizeyi C++'da tam sayıya dönüştürün

C++ programlama dilinde dize verilerini tam sayılara dönüştürmek için farklı yöntemler.

  1. Stringstream sınıfını kullanma
  2. stoi() işlevini kullanma
  3. atoi() işlevini kullanma
  4. sscanf() işlevini kullanma

Stringstream sınıfını kullanma

dizi akışı sayısal bir dizeyi int türüne dönüştürmek için kullanılan bir sınıftır. Stringstream sınıfı, bir akış nesnesi olarak bir dize eklemek için bir akış nesnesi bildirir ve ardından akışlara dayalı olarak dönüştürülen tamsayı verilerini çıkarır. Stringstream sınıfında, (<>) sol operatörden veri almak için kullanılan '<>' operatörleri bulunur.

C++ programlama dilinde dize verilerini bir tamsayıya dönüştürmek için stringstream sınıfını gösterecek bir program oluşturalım.

Java'da ekleme sıralaması

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

Çıktı

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

Yukarıdaki programda, bir obj nesnesi oluşturmak için stringstream sınıfını kullanıyoruz ve bu, string verilerinin bir tamsayıya dönüştürülmesine yardımcı oluyor. Daha sonra dönüştürülen dizeyi obj'den sayısal verilere çıkarmak için '<>' operatörünü kullanırız.

sscanf() işlevini kullanma

Bir sscanf() işlevi, belirli bir dizeyi tamsayı gibi belirli bir veri türüne dönüştürür.

Sözdizimi

özel karakter nedir
 sccanf ( str, %d, &amp;intvar); 

Sscanf() işlevi, dönüştürülen dizeyi depolamak için karakter dizesini (str), veri belirticisini (%d) ve tamsayı değişkenini (&intvar) belirtmek için üç bağımsız değişkene sahiptir.

sscanf() işlevinin algoritması

  1. sscanf() işlevi stringstream sınıfına aittir, dolayısıyla sınıfı programımıza aktarmamız gerekir.
  2. Sabit bir karakter dizesi str'yi başlatın.
  3. Dönüştürülen dizeyi tamsayı değerlerine tutmak için bir tamsayı değişkeni oluşturun.
  4. Dize değişkenini sscanf() işlevine aktarın ve işlev tarafından oluşturulan tamsayı değerini depolamak için sscanf() işlevini tamsayı değişkenine atayın.
  5. Tamsayı değerlerini yazdırın.

C++'da dizeyi sayısal sayıya dönüştürmek için sscanf() işlevinin kullanıldığı bir örneği ele alalım.

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

stoi() işlevini kullanma

stoi() işlevi, bir tamsayı değeri döndürmek için dizeyi parametre olarak ileterek bir dize verisini bir tamsayı türüne dönüştürür.

Sözdizimi

 stoi(str); 

stoi() işlevi bir str argümanı içerir. Dize verilerini bir tamsayı değerine dönüştürmek için str dizesi stoi() işlevinin içine iletilir.

stoi() fonksiyonunun algoritması

  1. Dize değerlerini depolamak için dize değişkenini başlatın.
  2. Bundan sonra, stoi() işlevini kullanarak dizenin tamsayı türü verilere dönüştürülmesini saklayan bir tamsayı türü değişkeni oluşturur.
  3. Tamsayı değişken değerini yazdırın.

C++ programlama dilinde string değerini tamsayı tipine dönüştürmek için stoi() fonksiyonunu kullanacak bir program oluşturalım.

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

atoi() işlevini kullanma

Karakter dizesini bir tamsayı değerine dönüştürmek için atoi() işlevi kullanılır. Bir atoi() işlevi, tamsayı verilerini döndürmek için karakter türü dizesini iletir.

javascript çok satırlı dize

Sözdizimi

 atoi (const char *str); 

atoi() fonksiyonunun algoritması

  1. Bir dizeyi depolamak için işaretçi tipinde bir karakter dizisini başlatın.
  2. Bundan sonra, atoi() işlevini kullanarak dizenin tamsayı türü verilere dönüştürülmesini saklayan bir tamsayı türü değişkeni oluşturur.
  3. Tamsayı değişken değerini yazdırın.

C++ programlama dilinde string değerini tamsayı tipine dönüştürmek için atoi() fonksiyonunu kullanacak bir program oluşturalım.

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