Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;set&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main() { std::string temp, mystring; std::set&lt;std::string&gt; myset; while(std::getline(std::cin, temp)) mystring += temp + ' '; temp = ""; for (size_t i = 0; i &lt; mystring.length(); i++) { if (mystring.at(i) == ' ' || mystring.at(i) == '\n' || mystring.at(i) == '\t') { myset.insert(temp); temp = ""; } else { temp.push_back(mystring.at(i)); } } if (temp != " " || temp != "\n" || temp != "\t") myset.insert(temp); for (std::set&lt;std::string&gt;::iterator i = myset.begin(); i != myset.end(); i++) { std::cout &lt;&lt; *i &lt;&lt; std::endl; } return 0; } </code></pre> <p>Let's start at the top. First off, you need a few variables to work with. <code>temp</code> is just a placeholder for the string while you build it from each character in the string you want to parse. <code>mystring</code> is the string you are looking to split up and <code>myset</code> is where you will be sticking the split strings.</p> <p>So then we read the file (input through <code>&lt;</code> piping) and insert the contents into <code>mystring</code>. </p> <p>Now we want to iterate down the length of the string, searching for spaces, newlines, or tabs to split the string up with. If we find one of those characters, then we need to <code>insert</code> the string into the set, and empty our placeholder string, otherwise, we add the character to the placeholder, which will build up the string. Once we finish, we need to add the last string to the set.</p> <p>Finally, we iterate down the set, and print each string, which is simply for verification, but could be useful otherwise.</p> <p>Edit: A significant improvement on my code provided by <a href="https://stackoverflow.com/users/14065/loki-astari">Loki Astari</a> in a <a href="https://stackoverflow.com/questions/11146379/splitting-stdstring-and-inserting-into-a-stdset/11146391#comment14617346_11146391">comment</a> which I thought should be integrated into the answer:</p> <pre><code>#include &lt;set&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main() { std::set&lt;std::string&gt; myset; std::string word; while(std::cin &gt;&gt; word) { myset.insert(std::move(word)); } for(std::set&lt;std::string&gt;::const_iterator it=myset.begin(); it!=myset.end(); ++it) std::cout &lt;&lt; *it &lt;&lt; '\n'; } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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