fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. int main() {
  7. string s("1.657e-01,4.8552e-01,8.7585e-01");
  8. vector<double> data;
  9. for (istringstream iss(s); ; ) {
  10. double x;
  11. iss >> x;
  12. if ( ! iss) {
  13. cout << "Parse error" << endl;
  14. break;
  15. }
  16. data.push_back(x);
  17. char comma = 0;
  18. iss >> comma;
  19. if ( ! iss)
  20. break;
  21. if (',' != comma) {
  22. cout << "What is [" << comma << "]?" << endl;
  23. break;
  24. }
  25. }
  26.  
  27. cout << "Parsed " << data.size() << " pieces of data" << endl;
  28. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Parsed 3 pieces of data