title: Transpose of a 2D matrix using vectors in C++
body: <p>I am trying to find out the transpose of a 2D matrix using vectors. The following code is not working for me. I am trying to pass the matrix into a function named transpose(). What are the other ways I can do it? I appreciate if you point out my errors. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  void transpose(vector&lt;vector&lt;int&gt; &gt; &amp;b)
  {
      vector&lt;vector&lt;int&gt; &gt; trans_vec;
  
      for(int i=0;i&lt;b.size();i++)
      {
          for(int j=0;j&lt;b[i].size();j++)
          {
              trans_vec[j][i]=b[i][j];
          }
      }
  }
  
  int main()
  {
      vector&lt;vector&lt;int&gt; &gt; v1;
      for(int i=0;i&lt;2;i++)
      {
          vector&lt;int&gt; temp;
          for(int j=0;j&lt;3;j++)
          {
              temp.push_back(i);
          }
          v1.push_back(temp);
      }
      //Display output
      for(int i=0;i&lt;v1.size();i++)
      {
          for(int j=0;j&lt;v1[i].size();j++)
          {
              cout&lt;&lt;v1[i][j];
          }
          cout&lt;&lt;endl;
      }
      transpose(v1);
      //display transposed matrix
      for(int i=0;i&lt;v1.size();i++)
      {
          for(int j=0;j&lt;v1[i].size();j++)
          {
              cout&lt;&lt;v1[i][j];
          }
          cout&lt;&lt;endl;
      }
      return 0;
  }
  </code></pre>