char,int,string之间的相互转换

  • string转int:

 stoi()函数

  • int转string

to_string()函数

  • char转int

-‘0’

  • int转char

+‘0’

#include
#include
using namesapce std;
int main() {
	/* ->string to int<- */
	string s = "1234";
	int n = stoi(s);
	cout << n << endl;
	/* ->int to string<- */
	n = 1234;
	s = to_string(n);
	cout << s << endl;
	/* ->char to int<- */
	char c = '2';
	n = c - '0';
	cout << n << endl;
	/* ->int to char<- */
	n = 5;
	c = n + '0';
	cout << c << endl;
	cout << "int and char, one -'0' and another +'0'"<

 

你可能感兴趣的:(C++学习)