-
Notifications
You must be signed in to change notification settings - Fork 0
8. String to Integer (atoi) #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| } | ||
| return num; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いですが、long long を使わずオーバーフローをしないようにチェックしつつ計算するパターンも書いてみると勉強になると思います。
| [英語版のドキュメント](https://en.cppreference.com/w/cpp/string/byte/isdigit.html)には | ||
| "isdigit and isxdigit are the only standard narrow character classification functions that are not affected by the currently installed C locale." | ||
| ってある。 | ||
| この二つって別に両立するんでしょうか?それとも日本語版のドキュメントが間違ってるんですかね(分かる方教えてください。。。) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
今回のようにASCII範囲の数字であればどの文字コードでも共通のコードポイントの認識です。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ソースコードの文字コードと実行環境の文字コードという概念があって、実行環境の文字コードは ASCII を包含していることは保証はされていません。たとえば、EBCDIC という文字コードがあります。
isdigit の仕様を追っていくと、たしかに、C99 では isdigit はロケールによらず 5.2.1 節に列挙されている10文字を指しているので、文字コードが何かはともかく、文字としてその10文字です。
ドラフト: http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。貼っていただいたドラフトのp205にも
The only functions in 7.4 whose behavior is not affected by the current locale are isdigit and isxdigit.
とあるので、この二つはロケールに影響されなさそうですね。
| {'8', 8}, | ||
| {'9', 9}, | ||
| }; | ||
| string_view removeLeadingWhitespace(string_view s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分ならメンバー変数やメンバー関数のあいだに空行を空けると思います。
ryosuketc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
よいと思います。long long を使わない方法についてはたぶんこの問題のポイントのひとつなのでやっておいたほうが良さそうです。
https://leetcode.com/problems/string-to-integer-atoi/description/