int string to convert each other

Source: Internet
Author: User

I, Itoa () and Atoi ()

Note: These two functions are not standard C functions, but are specific to Windows environments.

1, Itoa

#include <iostream>#include<string>using namespacestd;intMain () {intnum=12345; stringstr; Chars[ -]; Itoa (Num,s,Ten); STR=str+s; cout<<str; return 0;}

Note here: Use Itoa is sure to use the char[] over, if the direct use of string, it may be because there is no early application of memory reasons, although debugging time has a value, but the actual output is not value.

The following code does not have any output.

#include <iostream>#include<string>using namespacestd;intMain () {intnum=12345; stringstr; //Char s[30];    strings; Itoa (num, (Char*) S.c_str (),Ten); STR=str+s; cout<<str; return 0;}

2, Atoi

#include <iostream>#include<string>usingnamespace  std; int Main () {    int  num;     string str="12345";    num=atoi (str.c_str ());    cout<<num;     return 0 ;}

Second, StringStream

Consider StringStream as a universal type that can swallow any type of data or output any type of data based on the target type.

Note: 1, use is included header file Sstream

2. When using the same StringStream variable more than once, use the. Clear () method to clear the middle.

#include <iostream>#include<string>#include<sstream>using namespacestd;intMain () {intnum_in=12345; stringstr_in="45678"; stringstr_out; stringnum_out;    StringStream SS; SS<<num_in; SS>>str_out;    Ss.clear (); SS<<str_in; SS>>num_out; cout<<str_out<<num_out; return 0;}

int string to convert each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.