Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I change this vector function to accept more elements?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/236129/splitting-a-string-in-c">Splitting a string in C++</a> </p> </blockquote> <p>I have this vector function from one of the answers I got on my question</p> <pre><code>vector&lt;string&gt; split(const string&amp; s, char delim) { vector&lt;string&gt; elems(2); string::size_type pos = s.find(delim); elems[0] = s.substr(0, pos); elems[1] = s.substr(pos + 1); return elems; } </code></pre> <p>However, it only accept 2 elements. How do I modify it to accept based on how many delimiter s exist in the string s?</p> <p>e.g if I have this:</p> <pre><code>user#password#name#fullname#telephone </code></pre> <p>sometime the size might differ.</p> <p>How can I make this function flexible to work no matter how many elements, and able to split like this function above?</p> <p><strong>Just to further explain my problem:</strong></p> <p>What i wanna achieve is the capability to split using this vector function, of the same delimiter to N size instead of fixed at size 2.</p> <p>This function can only split maximum 2 element in a string, more than that result in Segmentation core dump</p> <p>as previously i only have needs for usage like </p> <pre><code>user:pass </code></pre> <p>now i added more attribute so i need to be able to split </p> <pre><code>user:pass:name:department </code></pre> <p>which x[2] and x[3] will return respectively name and department</p> <p>they all will be using same delimiter.</p> <p><strong>Further Update:</strong></p> <p>I tried using this function provided by 1 of the answer below</p> <pre><code>vector&lt;string&gt; split(const string&amp; s, char delim) { bool found; vector&lt;string&gt; elems; while(1){ string::size_type pos = s.find(delim); if (found==string::npos)break; elems.push_back(s.substr(0, pos)); s=s.substr(pos + 1); } return elems; } </code></pre> <p>and i get some error</p> <pre><code>server.cpp: In function ‘std::vector&lt;std::basic_string&lt;char&gt; &gt; split(const string&amp;, char)’: server.cpp:57:22: error: passing ‘const string {aka const std::basic_string&lt;char&gt;}’ as ‘this’ argument of ‘std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp; std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;, std::basic_string&lt;_CharT, _Traits, _Alloc&gt; = std::basic_string&lt;char&gt;]’ discards qualifiers [-fpermissive] </code></pre>
    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