String to Integer (atoi), integeratoi

Source: Internet
Author: User

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 ):
The signature ofC++Function had been updated. If you still see your function signature acceptsconst char *Argument, please click the reload button to reset your code definition.

Class Solution {public: // convert string to int. Be sure to judge that the Int range is exceeded 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 ++;} // The conversion result may be out of the int range. long ret = 0; (; 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 ;}};

Note:

The erase function is prototype as follows:
(1) string & erase (size_t pos = 0, size_t n = npos );
(2) iterator erase (iterator position );
(3) iterator erase (iterator first, iterator last );
That is to say, there are three usage methods:
(1) erase (pos, n); Delete n characters starting from pos. For example, erase (0, 1) is to delete the first character.
(2) erase (position); delete a character at position (position is a string type iterator)
(3) erase (first, last); Delete the characters from first to last (both first and last are iterators)

The following is the definition of library functions:

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); }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.