title: What's the best way to add two matrices using overload?
body: <p>I have defined one matrix class, and overloaded the + operator to be able to add instances of this class together.</p>
  
  <pre><code>class Matrix{
  public:
  vector&lt;vector&lt;int&gt;&gt; a;
  
  
  Matrix &amp; operator+(const Matrix&amp; b)
  {                
      vector&lt;vector&lt;int&gt;&gt;::const_iterator it0=b.a.begin();
      vector&lt;vector&lt;int&gt;&gt;::iterator it1=this-&gt;a.begin();
      vector&lt;int&gt;::iterator it2=it1-&gt;begin();
      vector&lt;int&gt;::iterator it3=it1-&gt;end();
      vector&lt;int&gt;::const_iterator it01=it0-&gt;begin();
  
      for(it1;it1!=this-&gt;a.end();it1++)
          {
          it2=it1-&gt;begin();
          it3=it1-&gt;end();
          it01=it0-&gt;begin();
          it0++;
               // a.begin(),a.end(),b.begin(),ret.begin()
          std::transform(it2,it3,it01,it2,std::plus&lt;int&gt;());
      }
              return  *this;
  }
  
  };
  </code></pre>
  
  <p>But then, there is also another way of doing this,</p>
  
  <pre><code>class Matrix{
  public:
  vector&lt;vector&lt;int&gt; &gt; a;
  
  Matrix &amp; operator + (const Matrix &amp;y) {
  
   for (int m=0; m&lt;y.a.size(); ++m) {
      for (int n=0; n&lt;y.a[0].size(); ++n) {
          this-&gt;a[m][n] = this-&gt;a[m][n] + y.a[m][n];
      }
  }
  
  return *this;
  }};
  </code></pre>
  
  <p>The second form is shorter, but uses arrays directly, while the first one uses iterators. Maybe it's possible to do this with iterators in a shorter way, I'm not sure. I've tested with simple cases and they seem to be equally efficient. 
  What's the proper way of doing this?</p>