std::atoi, std::atol, std::atoll
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <cstdlib>
  | 
||
|   int       atoi( const char *str );  | 
||
|   long      atol( const char *str );  | 
||
|   long long atoll( const char *str );  | 
(C++11 起) | |
转译 str 所指向的字节字符串中的整数值。
舍弃任何空白符,直至找到首个非空白符,然后接收尽可能多的字符以组成合法的整数表示,并转换之为整数值。合法的整数值含下列部分:
- (可选) 正或负号
 - 数位
 
参数
| str | - | 指向要转译的空终止字节字符串的指针 | 
返回值
成功时为对应 str 内容的整数值。若转换出的值落在对应返回类型的范围外,则返回值未定义。若不能进行转换,则返回 0 。
示例
运行此代码
#include <iostream> #include <cstdlib> int main() { const char *str1 = "42"; const char *str2 = "3.14159"; const char *str3 = "31337 with words"; const char *str4 = "words and 2"; int num1 = std::atoi(str1); int num2 = std::atoi(str2); int num3 = std::atoi(str3); int num4 = std::atoi(str4); std::cout << "std::atoi(\"" << str1 << "\") is " << num1 << '\n'; std::cout << "std::atoi(\"" << str2 << "\") is " << num2 << '\n'; std::cout << "std::atoi(\"" << str3 << "\") is " << num3 << '\n'; std::cout << "std::atoi(\"" << str4 << "\") is " << num4 << '\n'; }
输出:
std::atoi("42") is 42
std::atoi("3.14159") is 3
std::atoi("31337 with words") is 31337
std::atoi("words and 2") is 0参阅
|    (C++11)(C++11)(C++11)  | 
  转换字符串为有符号整数  (函数)  | 
|    (C++11)(C++11)  | 
  转换字符串为无符号整数  (函数)  | 
|    (C++11)  | 
  转换字节字符串为整数值  (函数)  | 
|    (C++11)  | 
  转换字节字符串为无符号整数值  (函数)  | 
|    (C++11)(C++11)  | 
  转换字节字符串为 std::intmax_t 或 std::uintmax_t   (函数)  | 
|    (C++17)  | 
  转换字符序列到整数或浮点值  (函数)  |