title: Reading data from a text file into a 3D vector in C++
body: <p>Having a simple text based file format for a 3D data structure:</p>
  
  <pre><code>d d d d d
  d d d d d
  d d d d d
  d d d d d
  d d d d d
  -
  s s s s s
  s s s s s
  s s s s s
  s s s s s
  s s s s s
  -
  g g g g g
  g g g g g
  g g g g g
  g g g g g
  g g g g g
  -
  </code></pre>
  
  <p>Which represents a 5x5x3 data structure of short strings. I wonder how to efficiently load this data into a <code>std::vector&lt;std::vector&lt;std::vector&lt;std::string&gt;&gt;&gt;</code> type of structure?</p>
  
  <p>I tried this so far:</p>
  
  <pre><code>std::ifstream file("file.txt"); 
  std::vector&lt;std::vector&lt;std::vector&lt;std::string&gt;&gt;&gt; data;
  
  int lines = 0;
  std::string line;
  while(std::getline(file, line)){        
      std::stringstream stream(line);            
      if (line.size() &gt; 1){
          lines++;
          data.resize(lines);            
          data.back().push_back(std::vector&lt;std::string&gt;());
          std::string val;
          while(stream &gt;&gt; val){
              data.back().back().push_back(val);
          }            
      }              
  }
  </code></pre>
  
  <p>Which somewhat works, though only fills up the 3D vector in two dimensions. I am still not sure about the efficiency.</p>