c ++ string, int, char array conversion (simple method)

 

 

One. Number --string

(1) Digital to string (new method in c ++ 11)

Example:

int i = 9;

string str = to_string(i);

 

(2) String to digital

Stoi (); chair (); stoul (); adit (); stoull (), appar ()

Function prototype: int stoi (const string & str, size_t * idx = 0, int base = 10);

Example:

int a;

string str = "1010";

a = stoi (str, 0, 2); // Take 2 numbers from the 0 position

a is 10

 

two. string-char array

(1) String to char array

char *p;

string str = "hello";

p = str. c_str () ;

(2) char array to string

Direct transfer

char a[10] = "hello";

string str(a);

 

 

Published 59 original articles · Likes46 · Visits 30,000+

Guess you like

Origin blog.csdn.net/sinat_41852207/article/details/104743794