fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <string>
  4. #include <sstream>
  5. #include <limits>
  6. #include <map>
  7. struct Customer
  8. {
  9. double X;
  10. double Y;
  11. explicit Customer(double X = 0., double Y = 0.)
  12. : X(X), Y(Y)
  13. {
  14. }
  15. };
  16. template<typename CharT>
  17. std::basic_istream<CharT>& operator>> (std::basic_istream<CharT>& Stream, Customer& Object)
  18. {
  19. if(!(Stream >> Object.X) || !(Stream >> Object.Y))
  20. {
  21. throw std::invalid_argument(std::string("Couldn't parse input"));
  22. }
  23. return Stream;
  24. }
  25. template<typename CharT, typename Traits>
  26. std::basic_ostream<CharT, Traits>& operator<< (std::basic_ostream<CharT, Traits>& Stream, Customer const& Object)
  27. {
  28. ::std::basic_ostringstream<CharT, Traits> StrStream;
  29. StrStream.flags(Stream.flags());
  30. StrStream.imbue(Stream.getloc());
  31. StrStream.precision(Stream.precision());
  32.  
  33. StrStream << Object.X << '\t' << Object.Y;
  34.  
  35. return Stream << StrStream.str();
  36. }
  37. int main()
  38. {
  39. std::istream& Source = std::cin;
  40. Source.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  41. typedef std::map<unsigned, Customer> ListType;
  42. ListType List;
  43. {
  44. unsigned Nr;
  45. Customer InputCustomer;
  46. try
  47. {
  48. while((Source >> Nr) && (Source >> InputCustomer))
  49. {
  50. List[Nr] = InputCustomer;
  51. }
  52. }
  53. catch(std::invalid_argument const& Exception)
  54. {
  55. std::cerr << Exception.what();
  56. return -1;
  57. }
  58. }
  59. std::cout << "Nr.\tX\tY\n";
  60. for(ListType::const_iterator i = List.begin(); i != List.end(); ++i)
  61. {
  62. std::cout << i->first << '\t' << i->second << '\n';
  63. }
  64. }
Success #stdin #stdout 0s 3440KB
stdin
nr. X   Y 
1   91  5 
2   56  77 
3   79  89 
4   12  46 
5   97  22 
6   90  9 
7   80  30 
8   62  26 
9   5   45 
10  32  36
stdout
Nr.	X	Y
1	91	5
2	56	77
3	79	89
4	12	46
5	97	22
6	90	9
7	80	30
8	62	26
9	5	45
10	32	36