Timeline for Splitting std::string based on delimiter using only find and substr
Current License: CC BY-SA 3.0
21 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 30, 2018 at 16:35 | history | edited | Toby Speight | CC BY-SA 3.0 |
Spelling fixes
|
Apr 30, 2018 at 7:12 | vote | accept | Noor | ||
Apr 29, 2018 at 19:25 | comment | added | Incomputable | @JDługosz, I'm glad you challenged it, though. Now I have a better understanding of what's happening. Actually I do control flow analysis much better now. | |
Apr 29, 2018 at 19:24 | comment | added | Incomputable |
@JDługosz, about last point, std::string_view doesn't allow any modification through iterators. It is stated that iterator and const_iterator are the same type (1). About your first point, the former is next_pos , and the latter is prev_pos . If I put prev_pos inside of the loop condition, I'll get prev_pos = next_pos + delimeter.size(); in the last iteration, which will induce undefined behavior. May be changing it to < might solve it, but as it stands, it gains very little.
|
|
Apr 29, 2018 at 19:10 | comment | added | JDługosz |
@Incomputable while (next_pos != content.end()) and if (prev_pos != content.end()) is repeating the exact same condition that the loop does. result.emplace_back(prev_pos, next_pos); (in the normal body of the loop) is repeated exactly in a special case. auto prev_pos = content.begin(); makes prev_pos a non-const iterator that could be used to modify content .
|
|
Apr 29, 2018 at 19:03 | comment | added | Incomputable | @JDługosz, sorry, could you clarify if your last comment applies to my post? I kind of get lost if there are no pings at the start of a comment. | |
Apr 29, 2018 at 18:54 | comment | added | JDługosz | In this version, you are testing for the end in two different places and appending to the vector in two different places. It is still taking the string parameters by value, and then not using const iterators into them. | |
Apr 29, 2018 at 18:52 | comment | added | JDługosz | @NoorNizar I went into detail about string_view in my new answer. | |
Apr 29, 2018 at 17:22 | comment | added | Incomputable |
@NoorNizar, std::string needs to copy into its own allocated memory (potentially calling new ), whereas string_view is very close to const char* pair which points to beginning and end of the string. Standard refers to this as "materialization", when temporary is passed to a function taking const & . Though it has some other, more important feature. It is related to template metaprogramming, topics of which is quite hard to explain.
|
|
Apr 29, 2018 at 17:14 | comment | added | Noor | Thank your for your answer. Learned alot of things from it actually . But one thing is . Why use string view ? Excuse my lack of understandings but isn't it doing the same as passing a string reference , With the &operator ? | |
Apr 29, 2018 at 16:26 | history | edited | Incomputable | CC BY-SA 3.0 |
added 280 characters in body
|
Apr 29, 2018 at 16:15 | history | edited | Incomputable | CC BY-SA 3.0 |
fixed the code to correspond to latest version
|
Apr 29, 2018 at 16:04 | history | edited | Incomputable | CC BY-SA 3.0 |
expanded paragraph about formatting and replaces s with content in example code
|
Apr 29, 2018 at 16:03 | comment | added | Incomputable |
@yuri, syncrhronizing the code in the post and the one in the link is very tiresome process. I just thought that it might be confusing if two versions don't match. EDIT: I expanded the paragraph about formatting. I'll try to replace s with something better, like content . EDIT2: done.
|
|
Apr 29, 2018 at 16:01 | comment | added | yuri |
Nice answer, could you maybe add a small explanation what exactly you find so unusual about the formatting in OPs code? I know it's just rough example code but I think you can probably choose a better variable name than s ;)
|
|
Apr 29, 2018 at 15:57 | history | edited | Incomputable | CC BY-SA 3.0 |
edited body
|
Apr 29, 2018 at 15:51 | history | edited | Incomputable | CC BY-SA 3.0 |
edited body
|
Apr 29, 2018 at 15:47 | vote | accept | Noor | ||
Apr 29, 2018 at 15:51 | |||||
Apr 29, 2018 at 15:46 | history | edited | Incomputable | CC BY-SA 3.0 |
edited body
|
Apr 29, 2018 at 15:44 | comment | added | Incomputable | I couldn't use English as well as C++ in this post. If you see any misspelling, or place where wording could be optimized, please comment or edit directly. I believe the code works, but any fixes are welcome too. | |
Apr 29, 2018 at 15:38 | history | answered | Incomputable | CC BY-SA 3.0 |