在C++中,可以使用强制类型转换或者使用标准库函数stoi()
将char
类型转换为int
类型。
char c = '5';
int num = static_cast<int>(c);
stoi()
函数:#include <string>
char c = '5';
int num = std::stoi(std::string(1, c));
这里通过将char
类型转换为std::string
类型,然后使用stoi()
函数将字符串转换为整数类型。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:c++中string转int的方法是什么