std::atoi
| Defined in header <cstdlib> |  |  | 
|---|---|---|
| int atoi( const char *str ); |  |  | 
| long atol( const char *str ); |  |  | 
| long long atoll( const char *str ); |  | (since C++11) | 
指向的字节字符串中的整数值。str...
丢弃所有空白字符,直到找到第一个非空白字符,然后尽可能多地使用字符来形成有效的整数表示,并将它们转换为整数值。有效整数值由以下部分组成:
- %28可选%29加或减符号
- 数字数字
参数
| str | - | pointer to the null-terminated byte string to be interpreted | 
|---|
返回值
的内容对应的整数值。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二次
另见
| stoistolstoll (C++11)(C++11)(C++11) | converts a string to a signed integer (function) | 
|---|---|
| strtolstrtoll | converts a byte string to an integer value (function) | 
| strtoul strtoull | converts a byte string to an unsigned integer value (function) | 
c Atoi、ATOL、环礁的文件
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

