title: C++ function to print a set
body: <p>Does anyone have an idea as to why</p>
  
  <pre><code>#include &lt;set&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  template &lt;typename T&gt;
  std::string set2Str(const std::set&lt;T&gt; &amp; S)
  {
      std::string retstr = "{";
      typename std::set&lt;T&gt;::const_iterator it(S.begin()), offend(S.end());
      if (it != offend)
          retstr.push_back(*it++);
      while (it != offend)
      {
          retstr.push_back(',');
          retstr.push_back(*it++);
      }
      retstr.push_back('}');
      return retstr;
  }
  
  int main()
  {
      std::set&lt;int&gt; mySet = {1, 5, 9, 69};
      std::cout &lt;&lt; set2Str(mySet);
  }
  </code></pre>
  
  <p>is outputting</p>
  
  <pre><code>{,,   ,E}
  </code></pre>
  
  <p>????</p>
  
  <p>Also, is there a more elegant way of writing the function <code>set2Str</code>? The fencepost problem with the commas makes my procedure ugly.</p>