C++string和int相互转化

int转string

include

int n = 0;
std::stringstream ss;
std::string str;
ss< ss>>str;

string转int

include

std::string str = "123";
int n = atoi(str.c_str());

将string每个字符都转化为int
string str = "123456";
for (int i = 0; i < str.length(); i++)
{
int a = str[i] - '0';
printf("%d \n", a);
}

你可能感兴趣的:(C++string和int相互转化)