c++ - How can I store in a vector<vector<double>> values from a File? -


values txt file

i have values .txt file, , want store in variable. in first row in file have 13 values, in others rows, , want store in next form:

vector<vector<double>> x; 

--first row

x[0][0] has value of first row , first col x[0][1] has value of first row , second col x[1][0] has value of second row , first col... , successively 

[edit]

i'm not sure i'm helping answer, since didn't provide problem, did not tried, , failed.

you should not expect people find solution problem, interested it, posted findings.

but not how forum should work, programming learning, , if ask without trying nor explaining thought process until not learn.

anyway, read answer 1 inspired, there key elements learn from.

[/edit]

this code inspired answer, should helpful understand key concepts of c++.

explanation emplace_back vs push_back.

explanation range-based loop: "for (auto : collection)"

#include <vector> #include <string> #include <fstream> #include <sstream> #include <iterator> #include <cassert> #include <iostream>  int main() {     std::vector< std::vector<double> > values;      std::ifstream ifs;      std::string line;      ifs.open("test.txt");      while(getline(ifs,line))      {         std::istringstream is(line);          std::vector<double> ns;          std::copy(std::istream_iterator<double>(is)                  , std::istream_iterator<double>()                   , std::back_inserter(ns));          assert(ns.size() > 1); //throw          values.emplace_back(std::vector<double>(ns.begin(), ns.end()));       }      (auto line : values)     {         (auto value: line)         {              std::cout << value << " ";          }         std::cout << std::endl;     } } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -