C++ 各种类型转换,char,char* , string,int互转

char 转 string

  • const char c = ‘a’;string s;s.push_back(c);

char[ ] 转 string

char* 转 string

  • char* p = "hello world", string str = p

string 转 char[ ]

 

string 转 char*

  • string str = ”hello world“,const char* p = str.c_str()

char 转 int

  • ascii码字符,如‘1’,可用‘1’-‘0’转化为整数

string 转 int

string str = "123"; int num = atoi(str.c_str());

int 转 char

int 转 string

# include

# include

string num = std::to_string(123)

你可能感兴趣的:(c++)