title: What data structure to use for reading CSV like files in C++?
body: <p>My task is to implement a hash join algorithm with tables (.txt files) provided in the program arguments.</p>
  
  <p>A table files could look like this:</p>
  
  <pre><code>c1:int,c2:int,c3:string,c4:long
  1,1,asd,11
  2,3,asdqwe,11
  </code></pre>
  
  <p>A header row, with the name of the column and type of the column, and then the rows delimited by ",".
  The number of columns and the type of the columns are variable.</p>
  
  <p>So the column name and type should be stored, and I must store these in a 2D array or matrix. </p>
  
  <p>I have to read the type and make a container for the column of the specified type. 
  I also have to store all the columns together in a heterogeneous array or struct.</p>
  
  <p>I don't know any of the STL containers below C++11 what is heterogenous.</p>
  
  <p>How can I determinate the type of a column, create a container from its type and store all the column arrays in a heterogeneous way?</p>
  
  <p>My try-hards:</p>
  
  <pre><code> class Table {
  public:
      struct Column {
          std::string name;
          std::string type;
  
          Column(std::string name, std::string type) : name(name), type(type) {}
      };
  
      //enum for switch-case
      static enum typeValue {
          INT,
          STRING,
          LONG,
          CHAR,
          DOUBLE,
          SHORT
      };
  
  
  private:
      std::string filePath;
      std::ifstream file;
      std::vector&lt;Column&gt; columns;
      int rowNumber;
      std::vector&lt;std::array&gt; tableData;
  
  public:
      Table(std::string filePath) {
          this-&gt;filePath = filePath;
          rowNumber = 0;
          open();
          loadTableData();
  
  
      }
  
  
      bool open() {
  
          file.open(filePath);
          std::string line;
          std::string delimiter = ",";
          size_t pos = 0;
          std::string token;
          getline(file, line);
          while ((pos = line.find(delimiter)) != std::string::npos) {     //parse the columns
              token = line.substr(0, pos);
              size_t posT = 0;
              posT = token.find(":");
              columns.push_back(Column(token.substr(0, posT), token.substr(posT + delimiter.length())));
              line.erase(0, pos + delimiter.length());
          }
          size_t posT = 0;
          posT = line.find(":");
          columns.push_back(Column(line.substr(0, posT), line.substr(posT + delimiter.length())));
  
          while (std::getline(file, line))    //count the rows
              ++rowNumber;
  
          for (int i = 0; i &lt; columns.size(); ++i) {
  
              switch (hashIt(columns[i].type)) {
                  case INT:
                      std::vector&lt;int&gt; *tempI = new std::vector&lt;int&gt;();
                      tableData[i] = tempI;
                      break;
                  case STRING:
                      std::vector&lt;std::string&gt; *tempS = new std::vector&lt;std::string&gt;();
                      tableData[i] = tempS;
                      break;
                  case LONG:
                      std::vector&lt;long&gt; *tempL = new std::vector&lt;long&gt;();
                      tableData[i] = tempL;
                      break;
                  case CHAR:
                      std::vector&lt;char&gt; *tempC = new std::vector&lt;char&gt;();
                      tableData[i] = tempC;
                      break;
                  case DOUBLE:
                      std::vector&lt;double&gt; *tempD = new std::vector&lt;double&gt;();
                      tableData[i] = tempD;
                      break;
                  case SHORT:
                      std::vector&lt;short&gt; *tempSh = new std::vector&lt;short&gt;();
                      tableData[i] = tempSh;
                      break;
                  default:
                      std::cerr &lt;&lt; "Error: Unsupported column type.";
              }
  
  
          }
  
          //file.close();
  
          std::cout &lt;&lt; "Table " &lt;&lt; filePath &lt;&lt; " has " &lt;&lt; columns.size() &lt;&lt; " columns." &lt;&lt; std::endl;
  
          return true;
  
  
      }
  
      bool loadTableData() {
          file.seekg(0, file.beg);
          std::string line;
          getline(file, line); //discarding the column headers
  
          while (std::getline(file, line)) {
              ++rowNumber;        //count the rows
  
              for (int i = 0; i &lt; columns.size(); ++i) {
  
                  std::string delimiter = ",";
                  size_t pos = line.find(delimiter);
                  std::string token = line.substr(0, pos);
                  tableData[i].push_back(token);
  
  
              }
          }
  
      }
  
      template&lt;typename T&gt;
  
      T **createColumnData(int colNum) {
          T *data = new T[rowNumber]();
          tableData.insert(colNum, data);
      }
  
  
      typeValue hashIt(std::string const &amp;inString) {
          if (inString == "int") return INT;
          if (inString == "string") return STRING;
          if (inString == "long") return LONG;
          if (inString == "char") return CHAR;
          if (inString == "double") return DOUBLE;
          if (inString == "short") return SHORT;
          std::cerr &lt;&lt; "Error: Unsupported column type.";
  
  
          return NULL;
  
      }
  
  
  };
  </code></pre>
  
  <p>Thank you!</p>