title: How to insert variable amount of data in 2d array in c++
body: <p>I have the following file input.txt which contains the values :</p>
  
  <pre><code>0 0
  0 1
  0 2
  0 3
  1 1
  1 4
  2 1
  2 4
  2 3
  </code></pre>
  
  <p>I want to insert these values to two arrays. I want them to look like this in the end.</p>
  
  <pre><code>0   0 1 2 3
  1   1 4
  2   1 4 3
  </code></pre>
  
  <p>First array will contain the id and second array will contain the elements. I have managed to dynamically allocate memory for these two arrays using the following : </p>
  
  <pre><code>int n,m,i=0,tempNum,lines;
  int NumberOfIds=0;
  ifstream read("input2.txt");
  while(read&gt;&gt;n&gt;&gt;m){
      if (i==0){
          tempNum=n;
      }
      if (tempNum != n){
          NumberOfIds++;
      }
      tempNum = n;
      i++;
  }
  lines=i-1;
  
  //printf("%d", j);
  int** idElements = new int*[NumberOfIds];
  int* ids = new int[NumberOfIds];
  int* numberOfIdElements = new int[NumberOfIds];
  
  // Rewinds file
  read.clear();
  read.seekg(0, ios::beg);
  
  int counter = 0;
  NumberOfIds=0;
  while(read&gt;&gt;n&gt;&gt;m){
  
      if (tempNum != n){
          numberOfIdElements[NumberOfIds] = counter;
          NumberOfIds++;
          counter = 0;
      }
      tempNum = n;
      counter++;
      i++;
  }
  
  for(int k = 0; k &lt; NumberOfIds; ++k)
      idElements[k] = new int[numberOfIdElements[k]];
  </code></pre>
  
  <p>Now I am stuck in how to insert the data. Any help would be appreciated.</p>