Conversation
| - 文字列の一部を置換 | ||
| - std::erase https://cpprefjp.github.io/reference/deque/deque/erase_free.html (c++20から) | ||
|
|
||
| 範囲や要素の指定は様々なので、使っていったりリファレンスを眺めて覚えていこう |
There was a problem hiding this comment.
c++にも色々な文字列操作の関数が用意されているんですね。
勉強になります。
| // stringを返り値にした時のコピーを防ぐため、出力用の空文字列を参照して更新する | ||
| static void canonicalizeEmail(const std::string& email, std::string& blank_for_canonicalized_email) { | ||
| auto at_position = email.rfind('@'); | ||
| auto local = email.substr(0, at_position); |
There was a problem hiding this comment.
好みの範囲かもしれませんが、localはusernameなどの方が直感的な気がします。
There was a problem hiding this comment.
自分は、localとdomainに分かれるというemailのルールに則っているのでこれでいい気がします
| } | ||
| return formatted_address; | ||
| } | ||
| }; |
There was a problem hiding this comment.
分かりやすかったです。
これも好みの範囲かもしれず恐縮ですが、is_ignoreはis_waiting_atなどの方が頭を使わずに理解できるような気がします。
| public: | ||
| int numUniqueEmails(std::vector<std::string>& emails) { | ||
| std::set<std::string> mailing_list; | ||
| for (const auto& adress : emails) { |
There was a problem hiding this comment.
ご指摘ありがとうございます!
スペルミスには気を付けます
| local = local.substr(0, plus_position); | ||
| std::erase(local, '.'); | ||
| auto domain = email.substr(at_position); | ||
| blank_for_canonicalized_email += local + domain; |
There was a problem hiding this comment.
ここは=ではダメなんでしょうか?
呼び出し側がblank_for_canonicalized_emailを空文字列以外で呼び出したときを考えて少し不安になりました。
There was a problem hiding this comment.
ご指摘ありがとうございます
おっしゃる通りで、空文字以外が入力された時も考えて、=にするほうが確実に良いと気づきました
| auto plus_position = local.find_first_of('+'); | ||
| local = local.substr(0, plus_position); | ||
| std::erase(local, '.'); | ||
| auto domain = email.substr(at_position); |
There was a problem hiding this comment.
好みですが、自分ならL164の下にこれ入れるかなと思います。
「localとdomainに分ける→localをいじる→localとdomainをくっつける」が自然な流れな気がしています。
There was a problem hiding this comment.
コメントありがとうございます
実際どちらも考えました。
domainが登場して何もないまま間が空くことを嫌って今の形を選択したのですが、どちらも好むという方がいるのが知れてよかったです
| - 部分文字列を取得 | ||
| - replace https://cpprefjp.github.io/reference/string/basic_string/replace.html | ||
| - 文字列の一部を置換 | ||
| - std::erase https://cpprefjp.github.io/reference/deque/deque/erase_free.html (c++20から) |
There was a problem hiding this comment.
よく使う erase は下のリンクではと思ったんですが、この PR で使っているのは std::erase ですね。
https://cpprefjp.github.io/reference/string/basic_string/erase.html
( C++03 から)
There was a problem hiding this comment.
ありがとうございます、貼っていただいたリンクはイテレータの範囲で削除するものと理解しました。
std::eraseは要素の値を直接指定して削除できるので便利だと感じました。
c++20など、新しいバージョンには便利な機能がほかにもありそうなので、キャッチアップしていこうと思います。
| - replace https://cpprefjp.github.io/reference/string/basic_string/replace.html | ||
| - 文字列の一部を置換 | ||
| - std::erase https://cpprefjp.github.io/reference/deque/deque/erase_free.html (c++20から) | ||
|
|
There was a problem hiding this comment.
ありがとうございます、そもそもこのような文字列含むstringの操作には#include <string>が必要ということに気が付いていませんでした。また、std::eraseには #include <algorithm> が必要でした。
また正規表現を使う際には regex が必要のようでした。
新しいメソッドを調べる際には、何をincludeするべきかにも気を付けます
| public: | ||
| int numUniqueEmails(std::vector<std::string>& emails) { | ||
| std::set<std::string> mailing_list; | ||
| for (const auto& adress : emails) { |
| return unique_emails.size(); | ||
| } | ||
| private: | ||
| // stringを返り値にした時のコピーを防ぐため、出力用の空文字列を参照して更新する |
There was a problem hiding this comment.
私は古い世代の人間なので、参照を使ってインプレースで更新する形を好むのですが、現代的には、RVO と move などで最適化がより効くようになったので、単に return にしてしまうのはよく見ます。
今回の場合、特に、最後で必ず local + domain で文字列が一回構築されるし、用法からしても比較的速度がクリティカルではないコードではないのかなと推察します。このあたりは正解があるものではないですが、いろいろな要素を比較して「パレート最適」にはしておき、あとは好みで選ぶところでしょう。
パレート最適:
https://discord.com/channels/1084280443945353267/1237649827240742942/1359594009168973844
There was a problem hiding this comment.
ありがとうございます。
まずはいろいろな手法の引き出しを身に着け、また何かを犠牲にせずとも改善できないかを考えながら、「パレート最適」に近づけるようにしたいです。そのうえで「パレート最適」のなかで、シチュエーションに応じた選択をできることを目標にします。
昔習ったパレート最適や生産可能性フロンティアを思い出して懐かしくなりました
There was a problem hiding this comment.
@irohafternoon
参考にですが、move等は「Effective Modern C++」に記載されております。この会で時々推薦されている本になります🙇
| return mailing_list.size(); | ||
| } | ||
| private: | ||
| static std::string formatAddress(const std::string& adress) { |
There was a problem hiding this comment.
参考にですが、GoogleGuideでは関数名をアッパーキャメルとしております🙇
https://google.github.io/styleguide/cppguide.html#Function_Names
This Problem
Unique Email Addresses
Next Problem
First Unique Character in a String