tholoz
7/24/2018 - 8:44 AM

Read std::vector<std::vector<double>> from csv

Read std::vector<std::vector> from csv

Basic solution. Be careful with encodings, UTF-8 is okay while UTF-16 doesn't work.

std::vector<std::vector<double>> data;
 
     std::ifstream file("file.csv");
     std::string line;
     std::vector<double> vect;
 
 
     while (file.good())
     {
         getline (file, line);
         if(line.size() == 0){
             break;
         }
 
         std::stringstream ss(line);
         double d = 0.0;
         while (ss >> d)
         {
             vect.push_back(d);
 
             if (ss.peek() == ',')
                 ss.ignore();
         }
         data.push_back(vect);
         vect.clear();
     }
 
     std::cout << "number of vectors:" << data.size() << std::endl;
 
     std::cout << data[0][0] << std::endl;