Cat*_*lus 10
boost::lexical_cast<int>("42");
Run Code Online (Sandbox Code Playgroud)
或者(C++ 11):
std::stoi("42");
Run Code Online (Sandbox Code Playgroud)
另外,不要使用char数组,除非它是互操作的.请std::string改用.也不要使用ato*功能,即使在C中,它们也会按设计破坏,因为它们无法正确发出错误信号.
自己编写这样的函数是一个很好的练习:
unsigned parse_int(const char * p)
{
unsigned result = 0;
unsigned digit;
while ((digit = *p++ - '0') < 10)
{
result = result * 10 + digit;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
当然,您应该更喜欢现实世界代码中现有的库设施.
| 归档时间: |
|
| 查看次数: |
10905 次 |
| 最近记录: |