std::atoi, std::atol, std::atoll
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <cstdlib> 中定义
|
||
int atoi( const char *str ); |
||
long atol( const char *str ) |
||
long long atoll( const char *str ); |
(C++11 起) | |
解释一个整数值,一个字节串所指向的
str
.原文:
Interprets an integer value in a byte string pointed to by
str
.函数丢弃任何空白字符,直到找到第一个非空白字符。然后它会尽可能多的字符可能形成一个有效的整数表示,并将其转换为整数值。有效的整数值由以下几部分组成:1
原文:
Function discards any whitespace characters until first non-whitespace character is found. Then it takes as many characters as possible to form a valid integer number representation and converts them to integer value. The valid integer value consists of the following parts:
- (可选)加号或减号
- 数字
目录 |
[编辑] 参数
str | - | null结尾的字节串的指针进行解释
原文: pointer to the null-terminated byte string to be interpreted |
[编辑] 返回值
整型值对应的内容
str
成功。如果转换的值落在相应的返回类型的范围,则返回值是不确定的。如果没有可以进行转换,0返回.原文:
Integer value corresponding to the contents of
str
on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, 0 is returned.[编辑] 示例
#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) |
将字符串转换为一个有符号整数 原文: converts a string to an signed integer (函数) |
一个字节的字符串转换为整数的值 原文: converts a byte string to an integer value (函数) | |
一个无符号整数的值转换为字节字符串 原文: converts a byte string to an unsigned integer value (函数) | |
C documentation for atoi, atol, atoll
|