fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. struct Node
  8. {
  9. int numlinks;
  10. std::string name;
  11.  
  12. Node(int num, std::string n) : numlinks(num), name(n)
  13. {
  14. }
  15.  
  16. friend std::ostream &operator<<(std::ostream &o, Node &node)
  17. {
  18. return o << node.name << " has " << node.numlinks << " links";
  19. }
  20. };
  21.  
  22. int main()
  23. {
  24. std::vector<std::vector<Node> > distances;
  25. //std::ifstream MyFile ("file.csv");
  26. std::istream &MyFile = std::cin;
  27. //if(MyFile.is_open())
  28. {
  29. std::string line;
  30. while(std::getline(MyFile, line))
  31. {
  32. std::istringstream row (line);
  33. distances.push_back(std::vector<Node>());
  34. std::string cell;
  35. while(std::getline(row, cell, ','))
  36. {
  37. int dist;
  38. std::istringstream(cell) >> dist;
  39. distances.back().push_back(Node(dist, "name"));
  40. }
  41. }
  42. }
  43.  
  44. for(std::size_t i = 0; i < distances.size(); ++i)
  45. {
  46. for(std::size_t j = 0; j < distances[i].size(); ++j)
  47. {
  48. std::cout << distances[i][j] << ", " << std::flush;
  49. }
  50. std::cout << std::endl;
  51. }
  52. }
Success #stdin #stdout 0s 3044KB
stdin
0,1,2,3,4
4,3,2,1,0
9,8,7,6,5
5,6,7,8,9
stdout
name has 0 links, name has 1 links, name has 2 links, name has 3 links, name has 4 links, 
name has 4 links, name has 3 links, name has 2 links, name has 1 links, name has 0 links, 
name has 9 links, name has 8 links, name has 7 links, name has 6 links, name has 5 links, 
name has 5 links, name has 6 links, name has 7 links, name has 8 links, name has 9 links,