String to Integer (atoi)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了String to Integer (atoi)相关的知识,希望对你有一定的参考价值。

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

class Solution {
public:
          // 字符串转int,注意判断超出Int范围
    int myAtoi(string str) {
        if (str == "") return 0;
        // 去首尾空格
        str.erase(0, str.find_first_not_of(‘ ‘));
        str.erase(str.find_last_not_of(‘ ‘) + 1);
        
        int i = 0, len = str.length(), sign = 1;
        while ( i < len && str[i] == ‘ ‘) i++;
        if (i == len) return 0;
        if (str[i] == ‘+‘) {
            sign = 1;
            i++;
        } else 
            if (str[i] == ‘-‘) {
                sign = -1;
                i++;
            }
        // 转换结果可能超出int范围,使用long long作为转换后接收变量的类型
        long long ret = 0;
        for (; i < len; i++) {
            if (str[i] < ‘0‘ || str[i] > ‘9‘) break;
            ret = ret * 10 + (str[i] - ‘0‘);
            if (ret > INT_MAX) break;
        }
        ret *= sign;
        if (ret > INT_MAX) return INT_MAX;
        if (ret < INT_MIN) return INT_MIN;
        return ret;
    }
};

注释:

erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

以下是库函数的定义:

stl: 
template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
           typename _Pointer = _Tp*, typename _Reference = _Tp&>
    struct iterator
    {
      /// One of the @link iterator_tags tag [email protected]
      typedef _Category  iterator_category;
      /// The type "pointed to" by the iterator.
      typedef _Tp        value_type;
      /// Distance between iterators is represented as this type.
      typedef _Distance  difference_type;
      /// This type represents a pointer-to-value_type.
      typedef _Pointer   pointer;
      /// This type represents a reference-to-value_type.
      typedef _Reference reference;
    };
string:
 iterator
      erase(iterator __first, iterator __last);
 
#if __cplusplus >= 201103L
      /**
       *  @brief  Remove the last character.
       *
       *  The string must be non-empty.
       */
      void
      pop_back() // FIXME C++11: should be noexcept.
      { erase(size()-1, 1); }

 

   

erase(iterator __first, iterator __last);

以上是关于String to Integer (atoi)的主要内容,如果未能解决你的问题,请参考以下文章

8. String to Integer (atoi)

[Leetcode] String to Integer (atoi)

[算法]String to Integer(atoi)

8. String to Integer (atoi)

[算法练习]String to Integer (atoi)

String to Integer (atoi)