fork(1) 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. std::copy(std::istream_iterator<int>(is), std::istream_iterator<int>(),
  23. std::back_inserter(ns));
  24.  
  25. assert(ns.size() > 1); // or throw something
  26.  
  27. // The first is the vertex
  28. map[ns[0]] = std::vector<int>(ns.begin() + 1, ns.end());
  29. }
  30.  
  31. for (auto const& pair: map)
  32. {
  33. std::cout << "Vertex " << pair.first << " has the following neighbour: ";
  34. std::copy(pair.second.begin(), pair.second.end(),
  35. std::ostream_iterator<int>(std::cout, " "));
  36. std::cout << "\n";
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3440KB
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