fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4.  
  5. // example data that imitates real file
  6. std::string example_data = R"([40, 12, 100, 3, 0.05 ,12 ,6]
  7.  
  8. [[3850.6, 1481, 592.4],
  9. [4989.4, 1919, 767.6],
  10. [3335.8, 1283, 513.2],
  11. [5142.8, 1978, 791.2],
  12. [5116.8, 1968, 787.2],
  13. [4460.3, 1715.5, 686.2],
  14. [3727.1, 1433.5, 573.4],
  15. [3248.7, 1249.5, 499.8],
  16. [4704.7, 1809.5, 723.8],
  17. [3127.8, 1203, 481.2],
  18. [3225.3, 1240.5, 496.2],
  19. [3403.4, 1309, 523.6]])";
  20.  
  21. // extractor operator to read vector from stream
  22. std::istream& operator>>(std::istream& is, std::vector<double>& vec)
  23. {
  24. //#1 - check if it starts from '['
  25. char c;
  26. is >> c;
  27. if (c != '[')
  28. throw std::runtime_error(std::string("Invalid character : ") + c +
  29. " when parsing vector of double");
  30. //#2 - get the line till ']'
  31. std::string line;
  32. if (!std::getline(is, line, ']'))
  33. throw std::runtime_error("Error parsing vector of double");
  34.  
  35. //#3 - parse values inside '[' and ']'
  36. std::istringstream lstr(line);
  37. std::string value;
  38. while (std::getline(lstr, value, ','))
  39. vec.push_back(stod(value));
  40. return is;
  41. }
  42.  
  43. std::istream& operator>>(std::istream& is, std::vector<std::vector<double>>& m)
  44. {
  45. //#1 - check if it starts from '['
  46. char c;
  47. is >> c;
  48. if (c != '[')
  49. throw std::runtime_error(std::string("Invalid character : ") + c +
  50. " when parsing matrix of double");
  51.  
  52. // parse matrix line-by-line
  53. while(true)
  54. {
  55. std::vector<double> tmp;
  56. is >> tmp;
  57. m.push_back(std::move(tmp));
  58. // if matrix finihed, c should contain ']', else - ','
  59. is >> c;
  60. if (c == ']')
  61. return is;
  62. if (c != ',')
  63. throw std::runtime_error(std::string("Invalid character : ") + c +
  64. " when parsing matrix of double");
  65. }
  66. }
  67. // display vector
  68. template<typename T>
  69. std::ostream& operator<<(std::ostream& os, const std::vector<T>& t)
  70. {
  71. os << "[";
  72. for (auto it = t.begin(); it != t.end(); ++it)
  73. os << (it != t.begin() ? ", " : "") << *it;
  74. os << "]";
  75. return os;
  76. }
  77. int main()
  78. {
  79. // here you can use any std::istream
  80. std::istringstream in(example_data);
  81.  
  82. std::vector<double> vec;
  83.  
  84. in >> vec;
  85.  
  86. std::cout << "Vector: " << vec << std::endl;
  87.  
  88. std::vector<std::vector<double>> m;
  89. in >> m;
  90.  
  91. std::cout << "Matrix: " << m << std::endl;
  92. return 0;
  93. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Vector: [40, 12, 100, 3, 0.05, 12, 6]
Matrix: [[3850.6, 1481, 592.4], [4989.4, 1919, 767.6], [3335.8, 1283, 513.2], [5142.8, 1978, 791.2], [5116.8, 1968, 787.2], [4460.3, 1715.5, 686.2], [3727.1, 1433.5, 573.4], [3248.7, 1249.5, 499.8], [4704.7, 1809.5, 723.8], [3127.8, 1203, 481.2], [3225.3, 1240.5, 496.2], [3403.4, 1309, 523.6]]