title: Trying to combine like terms for a templated polynomial class using recursion in c++
body: <p>I'm teaching my self C++.</p>
  
  <p>I'm trying to combine polynomials. For this I have defined straightforward classes:
  <code>Polynomial&lt;T&gt;</code>, <code>Term&lt;T&gt;</code> and <code>Coefficient&lt;T&gt;</code> (which may also just be
  <code>complex&lt;T&gt;</code>) using simple value composition. I have defined the required operator overloads.</p>
  
  <p>Polynomial's compare by sorting their terms (<code>std::sort</code>).</p>
  
  <p>I am working on <code>combineLikeTerms()</code>; This method when called will first call
  another member method that will sort this vector of Terms. For example: </p>
  
  <pre><code>4x^3 + 5x^2 + 3x - 4 
  </code></pre>
  
  <p>would be a possible resulting sorted vector. </p>
  
  <p><strong>Question</strong>:</p>
  
  <p>I am using two iterators on this vector and Im trying to merge adjacent terms
  of the same <em>order</em>.</p>
  
  <p>Lets say our initial vector after being sorted is this: </p>
  
  <pre><code>4x^3 - 2x^3 + x^3 - 2x^2 + x ...
  </code></pre>
  
  <p>after the function completes its iterations the temp stack vector would then
  look like this 2x^3 + x^3 - 2x^2 + x ... if we look there are still like terms
  this needs to be refactored again. </p>
  
  <p>How do I do this? I'm thinking of using recursion.</p>
  
  <pre><code>// ------------------------------------------------------------------------- //
  // setPolynomialByDegreeOfExponent()
  // should be called before combineLikeTerms
  template &lt;class T&gt;
  void Polynomial&lt;T&gt;::setPolynomialByDegreeOfExponent()
  {
      unsigned int uiIndex = _uiNumTerms - 1;
      if ( uiIndex &lt; 1 )
      {
          return;
      }
      struct _CompareOperator_
      {
          bool operator() ( math::Term&lt;T&gt; a, Term&lt;T&gt; b )
          {
              return ( a.getDegreeOfTerm() &gt; b.getDegreeOfTerm() );
          } // operator()
      };
      stable_sort( _vTerms.begin(), _vTerms.end(), _CompareOperator_() );
  } // setPolynomialByDegreeOfExponent
  
  // ------------------------------------------------------------------------- //
  // addLikeTerms()
  template &lt;class T&gt;
  bool Polynomial&lt;T&gt;::addLikeTerms( const Term&lt;T&gt;&amp; termA, const Term&lt;T&gt;&amp; termB, Term&lt;T&gt;&amp; result ) const
  {
      if ( termA.termsAreAlike( termB ) )
      {
          result = termA + termB;
          return true;
      }
      return false;
  } // addLikeTerms
  
  // ------------------------------------------------------------------------- //
  // combineLikeTerms()
  template &lt;class T&gt;
  void Polynomial&lt;T&gt;::combineLikeTerms()
  {
      // First We Order Our Terms.
      setPolynomialByDegreeOfExponent();
      // Nothing To Do Then
      if ( _vTerms.size() == 1 )
      {
          return;
      }
      Term&lt;T&gt; result; // Temp Variable
      // No Need To Do The Work Below This If Statement This Is Simpler
      if ( _vTerms.size() == 2 )
      {
          if ( addLikeTerms( _vTerms.at(0), _vTerms.at(1) )
      {
          _vTerms.clear();
              _vTerms.push_back( result );
          }
          return;
      }
      // For 3 Ore More Terms
      std::vector&lt;Term&lt;T&gt;&gt; vTempTerms; // Temp storage
      std::vector&lt;Term&lt;T&gt;&gt;::iterator it = _vTerms.begin();
      std::vector&lt;Term&lt;T&gt;&gt;::iterator it2 = _vTerms.begin()+1;
      bool bFound = addLikeTerms( *it, *it2, result );
  
      while ( it2 != _vTerms.end() )
      {
          if ( bFound )
          {
              // Odd Case Last Three Elems
              if ( (it2 == (_vTerms.end()-2)) &amp;&amp; (it2+1) == (_vTerms.end()-1)) )
              {
                  vTempTerms.push_back( result );
                  vTempTerms.push_back( _vTerms.back() );
                  break;
              }
              // Even Case Last Two Elems
              else if ( (it2 == (_vTerms.end()-1)) &amp;&amp; (it == (_vTerms.end()-2)) )
              {
                  vTempTerms.push_back( result );
                  break;
              }
              else
              {
                  vTempTerms.push_back( result );
                  it += 2;    // Increment by 2
                  it2 += 2;          "
                  bFound = addLikeTerms( *it, *it2, result );
              }
              }
                  else {
                  // Push Only First One
                  vTempTerms.push_back( *it );
                  it++;   // Increment By 1
                  it2++;         "
                  // Test Our Second Iterator
                  if ( it2 == _vTerms.end() )
                  {
                      vTempTerms.push_back( *(--it2) );  // same as using _vTerms.back()
                  }
                  else
                  {
                      bFound = addLikeTerms( *it, *it2, result );
                  }
              }
          }
          // Now That We Have Went Through Our Container, We Need To Update It
          _vTerms.clear();
          _vTerms = vTempTerms;
          // At This point our stack variable should contain all elements from above,
          // however this temp variable can still have like terms in it.
          // ??? Were do I call the recursion and how do I define the base case
          // to stop the execution of the recursion where the base case is a
          // sorted std::vector of Term&lt;T&gt; objects that no two terms that are alike...
          // I do know that the recursion has to happen after the above while loop
      } // combineLikeTerms
  </code></pre>
  
  <p>Can someone help me find the next step? I'd be happy to hear about any bugs/efficiency issues in the code shown.
  I love c++</p>