title: Removing Extra Whitespace in C++
body: <p>I'm trying to write code that reads in from a file, or standard input, and causes all whitespace in the file, or standard input, to be "squished", meaning that only the last whitespace character in a sequence of whitespace is printed.  My approach was to create a map with every word as a key and the whitespace after that word as it's value.  This might be problematic as I would need the map's contents to be printed in order of insertion and I would also have duplicate keys.  I found out about    std::unordered_multimap   but I am having trouble figuring out how I would go about implementing it.</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;vector&gt;
  #include &lt;iterator&gt;
  #include &lt;algorithm&gt;
  #include &lt;sstream&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  int main(int argc, char** argv) {
  
      string filename;
      char input;
      map&lt;string, string&gt; words;
      ostringstream os;
      string word = "";
      string spaces = "";
  
      filename = argv[argc-1];
      ifstream infile(filename);
  
      while ( infile.get(input) ) {
          os &lt;&lt; input;
      }
  
      string filecontents = os.str();
  
      for (int i = 0; i &lt; filecontents.length(); ++i) {
          if ( !isspace(filecontents[i])) {
              if (spaces.length() &gt;= 1) {
                  words[word] = spaces;
                  word = "";
                  spaces = "";
                  word += filecontents[i];
              }
  
              else {
                  word += filecontents[i];
              }
          }
  
          else {
              spaces += filecontents[i];
          }
      }
  
      words[word] = "";
  
      for (const auto&amp; p : words) {
          cout &lt;&lt; p.first &lt;&lt; p.second.back();
      }
  </code></pre>
  
  <p>File:</p>
  
  <pre><code>potato   milk   
  
  sausage
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>milk
  potato sausage
  </code></pre>
  
  <p>Maybe theres a better way to go about doing this?  I should add that I am new to C++, well, C in general.  Any help would be greatly appreciated.</p>