Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I <em>could</em> debug it for you, I guess but that won't help you in the long run. Here's what you do.</p> <p>After every line, put a printf() or cout staement dumping the changed variables to standard output. Then run your code, passing a simple set of parameters to it:</p> <pre><code>vector&lt;string&gt; x = split ("Hello there, Bob.", ' '); </code></pre> <p>Then, examine the output to see why your implementation isn't working. You'll probably have to break out of the code since, if it's just sitting there, you've probably got yourself one of those new-fangled infinite loops.</p> <blockquote> <p>Give a man a fish and he'll eat for a day, teach a man <em>to</em> fish, he'll never be hungry again.</p> </blockquote> <p>Or the Terry Pratchett version:</p> <blockquote> <p>Give a man some fire and he'll be warm for a day, set a man <em>on</em> fire, he'll be warm for the rest of his life.</p> </blockquote> <p><strong>Update:</strong></p> <p>Since you've stated that you've actually done what I suggested, here's what <em>I</em> found out from doing it. It's evident that when you set <code>beg</code> to <code>temp</code> at the end of the <code>while</code> loop, it's pointing at the space. That was discovered by printing the <code>beg</code> string at the top of the <code>while</code> loop - it never changed after the first word was extracted.</p> <p>Then, when you do the next <code>find</code>, it finds that exact same space rather than first skipping spaces then calling <code>find</code> properly. You need to skip the spaces after each <code>find</code>, making sure you don't iterate beyond the end of the string.</p> <p>This is my solution. Use it as you wish.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; using namespace std; vector&lt;string&gt; split( const string &amp;str, const char &amp;delim ) { typedef string::const_iterator iter; iter beg = str.begin(); vector&lt;string&gt; tokens; while(beg != str.end()) { //cout &lt;&lt; ":" &lt;&lt; beg._Myptr &lt;&lt; ":" &lt;&lt; endl; iter temp = find(beg, str.end(), delim); if(beg != str.end()) tokens.push_back(string(beg, temp)); beg = temp; while ((beg != str.end()) &amp;&amp; (*beg == delim)) beg++; } return tokens; } int main () { vector&lt;string&gt; x = split ("Hello, my name is Bob. ", ' '); return 0; } </code></pre> <p>Without that space-skipping code at the end of the <code>while</code> loop, the output was:</p> <pre><code>:Hello, my name is Bob. : : my name is Bob. : : my name is Bob. : : my name is Bob. : : my name is Bob. : : my name is Bob. : : my name is Bob. : : my name is Bob. : </code></pre> <p>and so on, ad infinitum. <em>With</em> the skipping code, you get:</p> <pre><code>:Hello, my name is Bob. : :my name is Bob. : :name is Bob. : :is Bob. : :Bob. : </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload