title: How to access value inside vector of vectors of type class
body: <p>I'm in an introductory C++ class and we've been tasked to write a program that reads a .ppm picture file, copies it, then writes it to an output file.</p>
  
  <p>To do this, my PpmPicture class has a vector of vectors, where each index contains a Pixel with red, green, and blue ints.</p>
  
  <p>My problem is with my output function, where I'm trying to output those ints to a file. How do I go about accessing them individually? Here is my code, you can see where I'm trying to output those values at the bottom in my writePpmFile function. I know the picture.red[0][0] doesn't make any sense, I was just trying to brute force a solution and that happened to be the last thing I tried.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  struct Pixel {
      Pixel();
      Pixel(int redValue, int greenValue, int blueValue);
      int red;
      int green;
      int blue;
  };
  
  Pixel::Pixel() {
      red = 0;
      green = 0;
      blue = 0;
  }
  
  Pixel::Pixel(int redValue, int greenValue, int blueValue) {
      red = redValue;
      green = greenValue;
      blue = blueValue;
  }
  
  class PpmPicture {
  public:
      PpmPicture();
      bool readPpmFile(string inputFile);
      int writePpmFile(string outputFile);
  private:
      vector&lt;vector&lt;Pixel&gt; &gt; picture;
      int rows;
      int columns;
      int intensity;
  };
  
  PpmPicture::PpmPicture() {
      rows = 0;
      columns = 0;
  }
  
  int main(int argc, char *argv[]) {
      PpmPicture myPic;
  
      if(argc != 3) {
          cout &lt;&lt; "usage: inputfile outputfile";
          return 0;
      }
  
      myPic.readPpmFile(argv[1]);
      myPic.writePpmFile(argv[2]);
  }
  
  bool PpmPicture::readPpmFile(string inputFile) {
      Pixel pix;
      vector&lt;Pixel&gt; tempArray;
      fstream fin;
      string fileType;
      int i, j;
  
      fin.open(inputFile.c_str());
      //Check if file opened
      if(fin.fail()) {
          return false;
      }
  
      while(!fin.eof()) {
          //Input first four values into appropriate variables
          fin &gt;&gt; fileType &gt;&gt; columns &gt;&gt; rows &gt;&gt; intensity;
          //Fill tempArray with pixel values
          while(fin &gt;&gt; pix.red &gt;&gt; pix.green &gt;&gt; pix.blue) {
              tempArray.push_back(pix);
          }
      }
  
      //Read file until you reach the number of rows specified by the file
      for(j = 0; j &lt; rows; j++) {
          //Input pixel values into each index in the row array
          //Enter new row when one row's width is achieved
          for(i = 0; i &lt; columns; i++) {
              picture[j].push_back(tempArray[i]);
          }
      }
  
      return true;
  }
  
  int PpmPicture::writePpmFile(string outputFile) {
      ofstream fout;
      int i, j, temp;
  
      fout.open(outputFile.c_str());
      if(fout.fail()) {
          return -2;
      }
  
      if(columns &lt; 0 || rows &lt; 0) {
          return -1;
      }
  
      fout &lt;&lt; "P3" &lt;&lt; endl;
      fout &lt;&lt; columns &lt;&lt; rows &lt;&lt; endl;
      fout &lt;&lt; intensity &lt;&lt; endl;
      fout &lt;&lt; picture.red[0][0];
      fout &lt;&lt; picture.green[0][0];
      fout &lt;&lt; picture.blue[0][0];
  
      /*for(j = 0; j &lt; rows; j++) {
          for(i = 0; i &lt; columns; i++) {
              fout &lt;&lt; picture[j][i] &lt;&lt; " ";
          }
      }*/
  
      return 0;
  }
  </code></pre>
  
  <p>I should add, like I said this is an introductory class so we haven't gone over a lot of shortcuts/different functions. So if possible keeping it somewhat simple (even if it isn't that efficient) would be preferred :)</p>