title: Getting a substream of a stringstream effectively
body: <p>I need to implement a mechanism where I can initialize a vector of my custom class using a text source, where each line of the source is representing one instance of my class. To achieve this, I implemented the <code>operator &gt;&gt;</code> for my class and <code>stringstream</code>. When I read the source, I go line-by-line and get a substream of my original source, then parse the substream each time. This has three benefits for me. First, this way I can make sure that one line of the text source would represent exactly one instance of my class. Second, as the rest of the line after parsing is ignored, I can safely add any comment in any line of my text source, which would surely get ignored by the parser. And third, I don't need to mention the length of the vector in my original source, since the first time I get a parsing error (I check the <code>fail</code> and <code>bad</code> bits of the stream to confirm this) I know that the vector declaration is over.</p>
  
  <p>To parse line-by-line, I'm using the following code:</p>
  
  <pre><code>std::stringstream       fullStream;
  std::stringstream       lineStream;
  std::string             str;
  bool                    isValid;
  myClass                 newInstance;
  std::vector &lt; myClass &gt; result;
  
  // Fill fullStream from external source (codepart omitted)
  isValid = true;
  while ( isValid &amp;&amp; ! fullStream.eof ( ) ) {
     std::getline ( fullStream, str );
     lineStream.clear ( );
     lineStream.str ( str );
     lineStream &gt;&gt; newInstance;
     isValid = ! lineStream.fail ( );
     if ( isValid ) {
        result.push_back ( newInstance );
     }
  }
  </code></pre>
  
  <p>Although this code works fine, I'm wondering if there was a better way to achieve the same result. Specially, if there was a more efficient way to extract a line from <code>fullStream</code> to <code>lineStream</code>.</p>
  
  <p>Thanks,<br />
  Ádám</p>