程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> String to Integer (atoi),integeratoi

String to Integer (atoi),integeratoi

編輯:C++入門知識

String to Integer (atoi),integeratoi


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 types@endlink.
      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); }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved