Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit function throws error
    primarykey
    data
    text
    <p>I'm trying to make a function to split a string, "Split At Spaces", into a vector which would contain "Split" "At" "Spaces". So far, this is the code I've got.</p> <pre><code>#include &lt;iostream&gt; #include &lt;utility&gt; #include &lt;algorithm&gt; using namespace std; std::vector&lt;std::string&gt; split(std::string * s, char * tosplit) { size_t i = 0; int count = 0; size_t contain; std::vector&lt;std::string&gt; split; std::cout &lt;&lt; "Start" &lt;&lt; std::endl; std::cout &lt;&lt; *s &lt;&lt; std::endl; std::cout &lt;&lt; *tosplit &lt;&lt; std::endl; while((contain = s-&gt;find(*tosplit,i)) != std::string::npos) { count++; i = contain + 1; } std::cout &lt;&lt; "Contains " &lt;&lt; count &lt;&lt; std::endl; if (count == 0) { std::cout &lt;&lt; "Equals 0" &lt;&lt; std::endl; split = std::vector&lt;std::string&gt;(1); split.at(0) = s-&gt;c_str(); return split; } split = std::vector&lt;std::string&gt;(count + 1); split.begin(); int lasti; i = s-&gt;find_first_of(*tosplit); split.at(0) = s-&gt;substr(0, i); lasti = i; int runs = 1; while (runs &lt;= count) { i = s-&gt;find(*tosplit, lasti + 1); std::cout &lt;&lt; i &lt;&lt; " " &lt;&lt; lasti &lt;&lt; std::endl; split.at(runs) = s-&gt;substr(lasti, --i); runs++; lasti = i; } split.at(runs) = s-&gt;substr(lasti, s-&gt;size()); std::cout &lt;&lt; "done, result is" &lt;&lt; std::endl; i = 0; while (i &lt; split.capacity()) { std::cout &lt;&lt; split.at(i) &lt;&lt; std::endl; i++; } return split; } </code></pre> <p>It throws an out_of_range exception. Any help you can give would be appreciated. This is like my first part using pointers in a function so I'm kinda guessing here.<br> Thanks!</p> <p>Please don't suggest using x or y method, <strong>I'd like to write my own</strong> as I'm doing it for the experience.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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