一、关于atoi atol的实现

  1. __BEGIN_NAMESPACE_STD
  2. __extern_inline double
  3. __NTH (atof (__const char *__nptr))
  4. {
  5. return strtod (__nptr, (char **) NULL);
  6. }
  7. __extern_inline int
  8. __NTH (atoi (__const char *__nptr))
  9. {
  10. return (int) strtol (__nptr, (char **) NULL, 10);
  11. }
  12. __extern_inline long int
  13. __NTH (atol (__const char *__nptr))
  14. {
  15. return strtol (__nptr, (char **) NULL, 10);
  16. }
  17. __END_NAMESPACE_STD

From /usr/include/stdlib.h

参考:

atoi: Convert string to integer

二、strtol

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.处理C语言字符串,将其内容作为一个某个特定base/进位制的整数,返回的是一个 long int值。如果参数endptr不是一个空指针,函数也会将enptr的值指向number的第一个字符。

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.这个函数首先会discard掉开头的(尽可能多的)whitespace/空格,直到第一个 非-空格出现。然后,从这个(非-空格)字符开始,取(尽可能多的、且符合base参数规则的)字符 并将这个字符解释为一个数值。

If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:

  1. An optional sign character (+ or -)
  2. An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
  3. A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present

If the base value is between 2 and 36, the format expected for the integral number is a succession of any of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix.

If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

For locales other than the "C" locale, additional subject sequence forms may be accepted.

strtol

关于atoi的实现的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 编写atoi库函数

    看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...

  3. 行程编码(atoi函数)

    #include<iostream> #include<string> #include<vector> using namespace std; void jie ...

  4. No.008:String to Integer (atoi)

    问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  6. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  7. [LeetCode] 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...

  8. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  9. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...

随机推荐

  1. charindex的用法

    declare @str nvarchar(50);set @str='462,464,2';select @str as '字符串'select len(@str) as '字符长度' select ...

  2. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践

    http://www.cnblogs.com/xishuai/p/ddd-repository-iunitofwork-and-idbcontext.html

  3. Microsoft .NET Pet Shop 4

    Microsoft .NET Pet Shop 4:将 ASP.NET 1.1 应用程序迁移到 2.0 299(共 313)对本文的评价是有帮助 - 评价此主题 发布日期 : 2006-5-9 | 更 ...

  4. maven依赖传递关系

    一.maven 依赖传递规则 举个例子,比如A依赖B,B依赖C,那么A也是依赖C的.A是对B的直接依赖,A对C是传递依赖 ①.最短路劲原则 如,路劲一:A依赖B,B依赖C,C依赖D(1.0.0): 路 ...

  5. MySQL内存表的特性与使用介绍

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. yarn 集群部署,遇到的问题小结

    版本号信息: hadoop 2.3.0  hive 0.11.0 1. Application Master 无法訪问     点击application mater 链接,出现 http 500 错 ...

  7. Xdebug+phpstorm配置

    首先,把自己参考的网上材料的连接黏贴出来,是英文的,但是讲解的很详细,有兴趣的同学可以看一下. 1.http://blog.jetbrains.com/webide/2011/02/zero-conf ...

  8. Jordan Lecture Note-4: Linear & Ridge Regression

    Linear & Ridge Regression 对于$n$个数据$\{(x_1,y_1),(x_2,y_2),\cdots,(x_n,y_n)\},x_i\in\mathbb{R}^d,y ...

  9. Paginator

    Paginator There are several ways to paginate items. The simplest is by using the paginate method on ...

  10. js中一些容易混淆的方法

    JavaScript中有一些名字十分冗长的函数名称,导致使用时会混乱,特此整理一番,加深印象. ①Object.getOwnPropertyDescriptor     --读取某个对象特定属性的属性 ...