fork download
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <map>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <sstream>
  7. #include <vector>
  8.  
  9. int main()
  10. {
  11. std::istream& ifs = std::cin; // use std::ifstream as you currently do
  12. // ifs.open(...); // ideone cannot read file so we read stdin instead
  13.  
  14. // vertex -> adjacent vertices
  15. std::map<int, std::vector<int>> map;
  16.  
  17. std::string line;
  18. while (std::getline(ifs, line))
  19. {
  20. std::istringstream is(line);
  21. std::vector<int> ns;
  22.  
  23. // assuming we know for sure the file is valid
  24. auto it = std::istream_iterator<int>(is);
  25. auto end = std::istream_iterator<int>();
  26. auto vertex = *(it++); // and not ++it !
  27. map[vertex] = std::vector<int>(it, end);
  28. }
  29.  
  30. for (auto const& pair: map)
  31. {
  32. std::cout << "Vertex " << pair.first << " has the following neighbour: ";
  33. std::copy(pair.second.begin(), pair.second.end(),
  34. std::ostream_iterator<int>(std::cout, " "));
  35. std::cout << "\n";
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3484KB
stdin
1 37 79 164 15 
2 123 134 10 141 13
stdout
Vertex 1 has the following neighbour: 37 79 164 15 
Vertex 2 has the following neighbour: 123 134 10 141 13