logo

C++ int til streng

Det er tre måter å konvertere et heltall til en streng:

forskjellen mellom array og arraylist
C++ int til streng
    Ved å bruke stringstream class Ved å bruke to_string() metoden Ved å bruke boost.lexical cast

Konvertering av et heltall til en streng ved å bruke stringstream-klassen.

Stringstream-klassen er en stream-klasse definert i header-filen. Det er en strømklasse som brukes til å utføre input-output-operasjoner på strengbaserte strømmer.

Følgende er operatørene som brukes til å sette inn eller trekke ut dataene:

rohit shetty skuespiller
    Operatør >>:Den trekker ut dataene fra strømmen.Operatør <<:< td>Den setter inn dataene i strømmen.

La oss forstå konseptet med operatører gjennom et eksempel.

    I setningen nedenfor setter << innsettingsoperatøren inn 100 i strømmen.
    stream1 << 100;I setningen nedenfor trekker >>-ekstraksjonsoperatøren ut dataene fra strømmen og lagrer dem i 'i'-variabelen.
    stream1 >> i;

La oss forstå gjennom et eksempel.

 #include #include using namespace std; int main() { int k; cout&lt;&gt;k; stringstream ss; ss&lt;&gt;s; cout&lt;<'
'<<'an integer value is : '<<k<<'
'; cout<<'string representation of an '<<s; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/c-int-string-2.webp" alt="C++ int to string"> <p>In the above example, we created the <strong>k</strong> variable, and want to convert the value of k into a string value. We have used the stringstream class, which is used to convert the k integer value into a string value. We can also achieve in vice versa, i.e., conversion of string into an integer value is also possible through the use of stringstream class only.</p> <p> <strong>Let&apos;s understand the concept of conversion of string into number through an example.</strong> </p> <pre> #include #include using namespace std; int main() { string number =&apos;100&apos;; stringstream ss; ss&lt;&gt;i; cout&lt;<'the value of the string is : '<<number<<'
'; cout<<'integer '< <i; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/c-int-string-3.webp" alt="C++ int to string"> <h3>Conversion of an integer into a string by using to_string() method.</h3> <p>The <strong>to_string()</strong> method accepts a single integer and converts the integer value or other data type value into a string.</p> <p> <strong>Let&apos;s understand through an example:</strong> </p> <pre> #include #include using namespace std; int main() { int i=11; float f=12.3; string str= to_string(i); string str1= to_string(f); cout&lt;<'string value of integer i is :'<<str<<'
'; cout<<'string f : '<< str1; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/c-int-string-4.webp" alt="C++ int to string"> <h3>Conversion of an integer into a string by using a boost.lexical cast.</h3> <p>The boost.lexical cast provides a cast operator, i.e., boost.lexical_cast which converts the string value into an integer value or other data type value vice versa.</p> <p> <strong>Let&apos;s understand the conversion of integer into string through an example.</strong> </p> <pre> #include #include using namespace std; int main() { int i=11; string str = boost::lexical_cast(i); cout&lt;<'string value of integer i is :'<<str<<'
'; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/c-int-string-5.webp" alt="C++ int to string"> <p>In the above example, we have converted the value of &apos;i&apos; variable into a string value by using lexical_cast() function.</p> <p>Let&apos;s understand the conversion of string into integer through an example. </p> <pre> #include #include using namespace std; int main() { string s=&apos;1234&apos;; int k = boost::lexical_cast(s); cout&lt;<'integer value of string s is : '<<k<<'
'; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/66/c-int-string-6.webp" alt="C++ int to string"> <p>In the above example, we have converted the string value into an integer value by using lexical_cast() function.</p></'integer></pre></'string></pre></'string></pre></'the></pre></'
'<<'an>