정수를 문자열로 변환하는 방법에는 세 가지가 있습니다.
stringstream 클래스를 사용하여 정수를 문자열로 변환합니다.
stringstream 클래스는 헤더 파일에 정의된 스트림 클래스입니다. 문자열 기반 스트림에서 입출력 작업을 수행하는 데 사용되는 스트림 클래스입니다.
다음은 데이터를 삽입하거나 추출하는 데 사용되는 연산자입니다.
예를 통해 연산자의 개념을 이해해 봅시다.
스트림1 << 100;
스트림1 >> i;
예를 통해 이해해 봅시다.
#include #include using namespace std; int main() { int k; cout<>k; stringstream ss; ss<>s; cout<<' '<<'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'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 ='100'; stringstream ss; ss<>i; cout<<'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'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<<'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'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<<'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 'i' variable into a string value by using lexical_cast() function.</p> <p>Let's understand the conversion of string into integer through an example. </p> <pre> #include #include using namespace std; int main() { string s='1234'; int k = boost::lexical_cast(s); cout<<'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>\\'string>\'string>\'the>' '<<'an>