title: Look for alternative solution for generating a sequence of data
body: <p>I have a problem, let's say:  </p>
  
  <blockquote>
    <p>Generate a new string from the previous
    one following by one rule.<br>
    1, 11, 21,
    1211, 111221, 312211, 13112221,...</p>
  </blockquote>
  
  <ol>
  <li>"1"  : means one number 1 so the next string will be "11"</li>
  <li>"11" : means two numbers 1 so the next string will be "21"</li>
  <li>"21" : means one number 2 and one number 1 so the next string will be "1211"
  ....</li>
  </ol>
  
  <p>And here is my solution:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class look_n_say {
  public:
      look_n_say( int n ) : n( n ) {
      }
  
      friend ostream&amp; operator &lt;&lt;( ostream&amp; out, const look_n_say&amp; rhs ) {
          string result;
          string init_str( "1" );
          out &lt;&lt; init_str &lt;&lt; endl;
          for( int i = 1; i &lt; rhs.n; ++i ) {
              result = rhs.get_new_str( init_str );   
              out &lt;&lt; result &lt;&lt; '\n';
              init_str = result;
              result.clear();
          }
          return out;
      }
  
  private:
      string get_new_str( const string&amp; str ) const {
          int cnt( 1 );
          string result;
          for( string::size_type i = 0, len = str.length(); i &lt; len; ++i ) {
              if( i &lt; len - 1 &amp;&amp; str[i] == str[i + 1] ) {
                  ++cnt;
              }
              else {
                  result.append( 1, cnt + 48 );
                  result.append( 1, str[i] );
                  cnt = 1;
              }
          }
          return result;
      }
  
  private:
      int n;
  };
  
  int main() {
      look_n_say lns( 10 );
      cout &lt;&lt; lns &lt;&lt; endl;
      return 0;
  }        
  </code></pre>
  
  <p>The reason that I'm not satisfied with my solution is that I missed an elegant solution using STL algorithm here. I thought of <code>std::transform</code>, but found no way to apply it. Is there any better way to solve this problem? Can anyone share with me? Any feedback and comments on my coding style would be greatly appreciated! Please tell me how bad my coding style was, and I will learn from that.</p>
  
  <p>Thanks,<br>
  Chan</p>