首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::atol

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​会被归还。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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、环礁的文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券