C++下char/string跟int型转换比较

QT5.6.1下C++的char/string跟int型数据大小比较

int a = 1;
char c = '1';
string s = "1";
//01string和int型比较——c_str + atoi
if(a == (atoi(s.c_str())))
{
    cout << "a = s" << endl;
}
else
{
     cout << "a != s" << endl;      
}
//02char跟int型比较
if(a == (c - '0'))
{
    cout << "a = c" << endl;
}
//或者
if(c == (a + '0'))
{
    cout << "a = c" << endl;  
}
//03char跟int型比较-需要先将char转换成const char *,再使用atoi
if(a == atoi((const char*)(c)))
{
    cout << "a = c" << endl;
}
//04Qt下面字符串还可以参考ByteArray

cpprefence.com
原文地址:https://www.cnblogs.com/BASE64/p/14363891.html