fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iterator>
  6. #include <istream>
  7. #include <algorithm>
  8.  
  9. struct Player
  10. {
  11. int playerID;
  12. std::string name;
  13. friend std::istream& operator >>(std::istream& is, Player& p);
  14. };
  15.  
  16. std::istream& operator >>(std::istream& is, Player& p)
  17. {
  18. is >> p.playerID;
  19. std::getline(is, p.name);
  20. return is;
  21. }
  22.  
  23. typedef std::vector<Player> PlayerVector;
  24. PlayerVector fillPlayers();
  25.  
  26. int main()
  27. {
  28. PlayerVector myPlayerPointer = fillPlayers();
  29. }
  30.  
  31. PlayerVector fillPlayers()
  32. {
  33. PlayerVector v;
  34. std::ifstream file1("Players.txt");
  35. std::copy(std::istream_iterator<Player>(file1),
  36. std::istream_iterator<Player>(), std::back_inserter(v));
  37. for (auto& p : v)
  38. std::cout << "PlayerID = " << p.playerID << " Name is " << p.name << "\n";
  39. return v;
  40. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
Standard output is empty